Skip to content Skip to sidebar Skip to footer

Only Allow One Of Two Columns To Be Set

I'm looking for a constraint in an SQL table that will only allow one of two nullable columns to be set per row. Both columns are foreign keys that relate to different tables. As a

Solution 1:

Add a constraint something like this...

ALTER TABLE TableName
ADD CONSTRAINT ck_Nullable CHECK (
                                    (CarID IS NULL AND PlaneID IS NOT NULL)
                                   OR 
                                    (CarID IS NOT NULL AND PlaneID IS NULL)
                                 )

Post a Comment for "Only Allow One Of Two Columns To Be Set"