且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除外键未引用的行

更新时间:2023-02-04 09:53:04

从本质上讲,SQL Server不会为约束保留2向信息,因此您唯一的选择是执行操作如果要删除该行,服务器将在内部进行操作:检查所有其他表。

At the heart of it, SQL servers don't maintain 2-way info for constraints, so your only option is to do what the server would do internally if you were to delete the row: check every other table.

如果(并且首先要确定)约束是简单的检查,则不要携带任何在删除级联上类型的语句时,您可以尝试从组表中删除所有内容。因此,任何删除的行都将没有引用它。否则,您会被Quassnoi的答案所困扰。

If (and be damn sure first) your constraints are simple checks and don't carry any "on delete cascade" type statements, you can attempt to delete everything from your group table. Any row that does delete would thus have nothing reference it. Otherwise, you're stuck with Quassnoi's answer.