Hi all,First I am sorry for ugly writing because my English is limited absolutely.Consider following family tree: David / | \ / | \ John Lisa Sarah | | James
I implement it into a table like this:--Designcreate table family_tree( node uniqeidentifier primary key, name nvharchar(25) sex bit, --when 1 then Male else Female parent uniqueidentifier null )--Dateinsert into family_treeselect newid(), 'David', 1, null
I’ve created a self-referenced to this table for check parent of a new row (node) be in node list, like this:alter table Family_Table with check add constraint [FK]foreign key ([parent]) refrences Family_Table([node])
But parent of a new row (node) cannot be a FEMALE node!To this reason I have created a very simple instead of insert trigger to achieve it like this:create trigger trg_I_Ion family_treeinstead of insert, updateasif exists(select * from tree where node=(select parent from inserted) and sex='M')--OR-->if exists(select * from inserted where parent in (select node from tree and sex='M')insert...
But are some major differences between my trigger and check constraint (For instance suppose we want to insert a group of rows; in check when a row is not inserted then fail…)I want a FK that only use a sub-set of PK data where have sex=Male;set of node--> {(David, M),(John, M),(Lisa, F),(Sarah,F)}sub-set of node that sex=M --> {(David, M),(John, M)}
please...