2011-08-26 75 views
0

假設我在表A中有一行我希望刪除,但在其他表中有多個外鍵約束。有沒有辦法自動生成刪除命令? 我認爲這是一個遞歸查看外鍵,然後生成一個刪除語句來處理所有外鍵的工具應該存在,但我找不到一個。有什麼辦法可以生成級聯刪除語句嗎?

我在MSSQL Server2008的

+3

你都知道了'上刪除cascade'選項FKS的? –

+0

http://msdn.microsoft.com/en-us/library/aa933119(v=sql.80).aspx – NullUserException

回答

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?

相關問題