MS SQL“ON DELETE CASCADE”多个外键指向同一个表?

前端之家收集整理的这篇文章主要介绍了MS SQL“ON DELETE CASCADE”多个外键指向同一个表?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
你好,
我有一个问题,我需要一个级联的多个外键指向同一个表..
[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)

通常有级联删除和大量外键,您需要花时间制定“级联”顺序,以便在“树”顶部发生的删除成功级联到引用表.但这是不可能的.

原文链接:https://www.f2er.com/mssql/82734.html

猜你在找的MsSQL相关文章