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

发布于 2024-12-01 19:04:32 字数 126 浏览 2 评论 0原文

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

我在 MSSql server2008 上

Let's say I have a row in Table A I wish to delete but there a multiple foreign key constraints in other tables. Is there a way to automatically generate the delete command?
I think a tool that would recursively look at the foreign keys and in turn generate a delete statement to take care of all foreign keys should exist, but I can't find one.

I'm on MSSql server2008

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

豆芽 2024-12-08 19:04:32

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

MSDN 级联引用完整性约束

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甚至有一个解决方案,您不将其添加到表中:

在 SQL 中Server 2005,我可以在不设置表属性的情况下进行级联删除吗?

When setting up your Foreign Key relationships there is an ON DELETE CASCADE you can add.

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 even has a solution where you are not adding it to the table:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文