question

BobbyP-1695 avatar image
0 Votes"
BobbyP-1695 asked EchoLiu-msft commented

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

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.

sql-server-transact-sql
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Could you please validate all the answers so far and provide any update?
If all of them are not working or helpful, please provide more sample data or details about your issue.
Please remember to accept the answers if they helped. Your action would be helpful to other users who encounter the same issue and read this thread.

Thank you for understanding!

0 Votes 0 ·
TomPhillips-1744 avatar image
0 Votes"
TomPhillips-1744 answered

See: https://docs.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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BobbyP-1695 avatar image
0 Votes"
BobbyP-1695 answered TomPhillips-1744 commented

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That was only an example.

You can change the rule to:
(seq_num IS NULL OR (seq_num = 65000 AND parent_seq_num IS NOT NULL))

0 Votes 0 ·
EchoLiu-msft avatar image
0 Votes"
EchoLiu-msft answered EchoLiu-msft edited

Hi @BobbyP-1695,

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".


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BobbyP-1695 avatar image
0 Votes"
BobbyP-1695 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ErlandSommarskog avatar image
0 Votes"
ErlandSommarskog answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.