In the following table, is there a way to ensure PreviousID always references an ID in a row with a matching ParentID, or, if ParentID is null, it is also null in the referenced row?
CREATE TABLE MyTable (
ID int not null identity(1,1) primary key,
ParentID int null foreign key references MyTable (ID),
PreviousID int null foreign key reference MyTable (ID),
foreign key (ParentID, PreviousID) references MyTable (ParentID, ID)
)
An example:
+-ID-+-ParentID-+-PreviousID-+ | 1 | NULL | NULL | | 2 | 1 | NULL | | 3 | NULL | 2 | <-- shouldn't be possible, should have to refer to ID where ParentID is null +----+----------+------------+
Is there a way to enforce this?
UPDATE: For those wondering, the compound foreign key doesn't enforce this for the following reason (copied from MSDN):
A FOREIGN KEY constraint can contain null values; however, if any column of a composite FOREIGN KEY constraint contains null values, verification of all values that make up the FOREIGN KEY constraint is skipped. To make sure that all values of a composite FOREIGN KEY constraint are verified, specify NOT NULL on all the participating columns.
UPDATE: In case it helps to visualize the data structure being represented, it's a B-tree where nodes having the same parent compose a linked list. I'm trying to enforce that each 'previous' pointer in the linked list points to another node with the same parent. In the case that the parent is null, it should point to another node where the parent is also null.