SQL Server Syntax with some good references to add a Table/Column Constraint

Bobby P 221 Reputation points
2021-11-05T16:13:33.007+00:00

We need to add a Constraint to one of our tables which will not allow a <NULL> to a data column based on the value of a [seq_num] because we want to start using this going forward...thus wanting to specify a [seq_num] to enforce going forward.

Can anyone tell me the exact Syntax to do so? Just want to make sure we get this right with all the Constraint options and what not.

And maybe site some good web references for CONSTRAINT Syntax.

Thanks in advance for your help.

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
{count} votes

5 answers

Sort by: Most helpful
  1. Tom Phillips 17,716 Reputation points
    2021-11-05T16:39:02.083+00:00

    See: https://learn.microsoft.com/en-us/sql/relational-databases/tables/unique-constraints-and-check-constraints?view=sql-server-ver15#Check

    CREATE TABLE testtbl (  
    	Id INT IDENTITY(1,1) NOT NULL,  
    	seq_num INT NULL,  
    	parent_seq_num INT NULL  
    )  
      
    ALTER TABLE testtbl  
    	ADD CONSTRAINT chkseqnum CHECK (seq_num IS NULL OR (seq_num IS NOT NULL AND parent_seq_num IS NOT NULL));    
      
    INSERT INTO testtbl (seq_num, parent_seq_num)  
    	VALUES (NULL,1)  
      
    INSERT INTO testtbl (seq_num, parent_seq_num)  
    	VALUES (2,NULL)  
      
      
    INSERT INTO testtbl (seq_num, parent_seq_num)  
    	VALUES (3,2)  
      
    SELECT *  
    FROM testtbl  
      
    
    0 comments No comments

  2. Bobby P 221 Reputation points
    2021-11-05T17:09:53.36+00:00

    But I want to base it on a specific number? So it should NOT be <NULL> and NOT ALLOW <NULL> after a [seq_num] is say 65000


  3. EchoLiu-MSFT 14,571 Reputation points
    2021-11-08T06:05:00.35+00:00

    Hi @Bobby P ,

    You can create a check constraint in a table to specify the data values that are acceptable in one or more columns in SQL Server by using SQL Server Management Studio or Transact-SQL.
    Please also chek:
    Create Check Constraints

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Regards,
    Echo


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

  4. Bobby P 221 Reputation points
    2021-11-08T17:05:57.237+00:00

    Why can't I grasp this?

    I created...

    ADD CONSTRAINT [CK_payment_cards_patient_num] CHECK ([patient_num] IS NULL
    OR
    (
    [payment_card_seq_num] > 67940
    AND [patient_num] IS NOT NULL
    )
    )
    ;

    And it still is allowing <NULL> for [patient_num] if the [payment_card_seq_num] is greater than 67940

    0 comments No comments

  5. Erland Sommarskog 101K Reputation points MVP
    2021-11-08T22:34:32.913+00:00

    Well, the constraint clearly says that patient_num being NULL is legal. What is not legal is to give a value to patient_num if payment_card_seq_num is less or equal to 67940.

    I think this is constraint you need:

    CHECK (NOT (patient_num IS NULL AND [payment_card_seq_num] > 67940))
    

    I have often found it easier to write CHECK constraints by explicitly expressing what I am disallowing.

    0 comments No comments