有没有办法生成级联删除语句?

时间:2011-08-26 21:50:54

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

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

我在MSSql server2008上

1 个答案:

答案 0 :(得分:6)

设置外键关系时,您可以添加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.

所以甚至有一个解决方案,你没有把它添加到表中:

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