Remove rows NOT referenced by a foreign key

前端 未结 2 1270
悲哀的现实
悲哀的现实 2021-02-09 11:42

This is somewhat related to this question:

I have a table with a primary key, and I have several tables that reference that primary key (using foreign keys). I need to

2条回答
  •  悲&欢浪女
    2021-02-09 12:19

    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.

    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.

提交回复
热议问题