I have the following table:
CREATE TABLE X ( A SOMETYPE NOT NULL, B SOMETYPE NOT NULL, C SOMETYPE NULL, PRIMARY KEY (A,B), FOREIGN KEY (A,C) REFERENCES X (A,B) );
Objects stored in X
are hierarchically organized: if there is a string (A1,B1,C1)
and C1 IS NOT NULL
, then it is considered a βchildβ of (A1,C1,C2)
any C2
. Since the element cannot descend from itself, I would like to make it illegal if there are cyclic hierarchical sequences:
-- legal INSERT INTO X (A1,B1,NULL); INSERT INTO X (A1,B2,B1); INSERT INTO X (A1,B3,B2); INSERT INTO X (A1,B4,B2); -- currently legal, but I want to make it illegal UPDATE X SET C = B1 WHERE B = B1; UPDATE X SET C = B2 WHERE B = B1; UPDATE X SET C = B3 WHERE B = B1; UPDATE X SET C = B4 WHERE B = B1; UPDATE X SET C = B2 WHERE B = B2; UPDATE X SET C = B3 WHERE B = B2; UPDATE X SET C = B4 WHERE B = B2; UPDATE X SET C = B3 WHERE B = B3; UPDATE X SET C = B4 WHERE B = B4;
How to do it?
Alternatively, I could add a field representing the "level" in the hierarchy to the table:
CREATE TABLE X ( A SOMETYPE NOT NULL, B SOMETYPE NOT NULL, C SOMETYPE NULL, LEVEL INT NOT NULL, PRIMARY KEY (A,B), FOREIGN KEY (A,C) REFERENCES X (A,B) );
Then I would like to require LEVEL
be 0
when C IS NULL
and parent LEVEL + 1
otherwise.
I am using SQL Server 2008 R2.