你好,
我有一个问题,我需要一个级联的多个外键指向同一个表..
我有一个问题,我需要一个级联的多个外键指向同一个表..
[Insights] | ID | Title | | 1 | Monty Python | | 2 | Spamalot | [BroaderInsights_Insights] | broaderinsight_id | insight_id | | 1 | 2 |
基本上,当一个或两个记录中的一个或两个在洞察表中被删除时,我需要关系也被删除..
我试过这个:
CREATE TABLE broader_insights_insights(id INT NOT NULL IDENTITY(1,1),broader_insight_id INT NOT NULL REFERENCES insights(id) ON DELETE CASCADE,insight_id INT NOT NULL REFERENCES insights(id) ON DELETE CASCADE,PRIMARY KEY(id)) Go
这导致警告级联“可能导致循环或多级联路径”
所以试试添加一个级联只是insight_id,这导致:
“DELETE语句与REFERENCE约束冲突”
有任何想法吗?
谢谢
丹尼尔
解决方法
您必须将其作为INSTEAD OF delete触发器来实现,才能使其得以实现.就像是:
create trigger T_Insights_D on Insights instead of delete as set nocount on delete from broader_insights_insights where insight_id in (select ID from deleted) or broader_insight_id in (select ID from deleted) delete from Insights where ID in (select ID from deleted)