【问题标题】:Is there any way to generate a cascade delete statement?有没有办法生成级联删除语句?
【发布时间】:2011-08-26 21:50:53
【问题描述】:

假设我在表 A 中有一行我想删除,但在其他表中有多个外键约束。有没有办法自动生成删除命令? 我认为应该存在一种可以递归查看外键并生成删除语句来处理所有外键的工具,但我找不到。

我在 MSSql server2008 上

【问题讨论】:

标签: sql sql-server sql-delete cascading-deletes


【解决方案1】:

在设置外键关系时,您可以添加 ON DELETE CASCADE

MSDN Cascading Referential Integrity Constraints

ON DELETE CASCADE

Specifies that if an attempt is made to delete a row with a key referenced by foreign 
keys in existing rows in other tables, all rows containing those foreign keys are also 
deleted. If cascading referential actions have also been defined on the target tables, 
the specified cascading actions are also taken for the rows deleted from those tables.

SO 甚至有一个解决方案,您不将其添加到表中:

In SQL Server 2005, can I do a cascade delete without setting the property on my tables?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 2017-11-17
    • 2012-10-06
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多