I have the following table definition:
CREATE TABLE X (
A SOMETYPE NOT NULL,
B SOMETYPE NOT NULL,
C SOMETYPE NULL,
PRIMARY KEY (A,B),
FOREIGN KEY (A) REFERENCES Y (A)
);
I want to add the following constraint: If C IS NOT NULL, then FOREIGN KEY (A,C) REFERENCES X (A,B). How do I do that (without using a trigger, if possible)?
I am using SQL Server 2008 R2, but this should be irrelevant to the question.
If I get what you want you need to have a primary key on A in table Y and a unique constraint on A,B in table Y.
Try this:
Test: