Reputation: 75
I have table well with name, code, and code2 fields. How to write check to allow only rows with name and one of code present present?
WellA, null, Code2 - OK
WellB, Code1, null - OK
WellC, Code3, Code4 - OK
WellD, null, null - NOT OK
Regards
Upvotes: 2
Views: 783
Reputation: 175716
You could add CHECK
constraint:
ALTER TABLE tab
ADD CONSTRAINT my_check CHECK (COALESCE(Code1,Code2) IS NOT NULL);
Upvotes: 3
Reputation: 246523
Add a check constraint:
CHECK (name IS NOT NULL AND
(code IS NOT NULL OR code2 IS NOT NULL))
Upvotes: 0