SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Check constraint on multiple columns


Check constraint on multiple columns

Author
Message
sqlpadawan_1
sqlpadawan_1
SSC Eights!
SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)

Group: General Forum Members
Points: 824 Visits: 1121
I have a case where if the Id field is a specific value, I don't want to allow null in another field, but if the Id value <> a specific value, null is ok.

In the example below, inserting the first record should succeed, the second should succeed, and the 3rd should fail. Right now the 2nd two fail. I gotta be missing something easy, but I can't figure it out.

Any ideas?


USE tempdb
GO

IF OBJECT_ID('tempdb.dbo.CheckConstraintTest') IS NOT NULL
DROP TABLE tempdb.dbo.CheckConstraintTest;
CREATE TABLE CheckConstraintTest
(
CTId INT NOT NULL ,
Goal INT NULL ,
CONSTRAINT CK_CheckConstraintTest_Goal CHECK ( CTId = 1
AND Goal IS NOT NULL )
);

INSERT INTO CheckConstraintTest
( CTId, Goal )
VALUES ( 1, 0 );

INSERT INTO CheckConstraintTest
( CTId, Goal )
VALUES ( 2, NULL );

INSERT INTO CheckConstraintTest
( CTId, Goal )
VALUES ( 1, NULL );



gmrose
gmrose
SSC-Addicted
SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)

Group: General Forum Members
Points: 416 Visits: 278
The first INSERT succeeds because both "CTId = 1" is TRUE and "Goal IS NOT NULL" is TRUE.
The second INSERT fails because "CTId = 1" is not TRUE.
The third INSERT fails because "Goal IS NOT NULL" is not TRUE.

Since you have "AND" in the test, both conditions must be TRUE for success.
Jason Selburg
Jason Selburg
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14157 Visits: 4137
Wrap your conditions in parenthesis and add an OR


CREATE TABLE CheckConstraintTest
(
CTId INT NOT NULL ,
Goal INT NULL ,
CONSTRAINT CK_CheckConstraintTest_Goal CHECK (
(CTId = 1 AND Goal IS NOT NULL )
OR
(CTId <> 1)
)
);



______________________________________________________________________

Personal Motto: Why push the envelope when you can just open it?

If you follow the direction given HERE you'll likely increase the number and quality of responses you get to your question.

Jason L. Selburg
sqlpadawan_1
sqlpadawan_1
SSC Eights!
SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)

Group: General Forum Members
Points: 824 Visits: 1121
Argh, I should have been able to figure that out.

Thanks!
Tom Thomson
Tom Thomson
SSC Guru
SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)

Group: General Forum Members
Points: 51132 Visits: 13160
Jason Selburg (7/25/2014)
Wrap your conditions in parenthesis and add an OR


CREATE TABLE CheckConstraintTest
(
CTId INT NOT NULL ,
Goal INT NULL ,
CONSTRAINT CK_CheckConstraintTest_Goal CHECK (
(CTId = 1 AND Goal IS NOT NULL )
OR
(CTId <> 1)
)
);


That's logicall correct but a bit redundant, you could just use

CREATE TABLE CheckConstraintTest
(
CTId INT NOT NULL ,
Goal INT NULL ,
CONSTRAINT CK_CheckConstraintTest_Goal CHECK (
(Goal IS NOT NULL)
OR
(CTId <> 1)
)
);



Tom

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search