2

I searched but everything I found either didn't answer the question, or I didn't really understand

I'm creating a table, and I want to put contraints in on some columns that will require it to be not null if another column has a certain value. What I have now is:

ColumnA NOT NULL CHECK (ColumnB = 8802 or ColumnB = 8804),

Basically if ColumnB is equal to either of those two values, ColumnA must have a value, and it can be null the rest of time.

4

1 回答 1

9

也许:

ALTER TABLE ADD CONSTRAINT A_B_Check
CHECK (
        ColumnA IS NOT NULL 
    OR (ColumnA IS NULL AND (ColumnB <> 8802 AND ColumnB <> 8804))
);
于 2013-06-24T15:16:01.513 回答