【发布时间】:2018-11-27 23:38:00
【问题描述】:
我有 2 张桌子。
create table A
(
idA int primary key,
.
.
)
create table B
(
idA int,
idB int primary key,
.
.
Foreign key (idA) A (idA) ON DELETE CASCADE
)
create Table AB
(
idA int,
idB int,
.
.
Foreign key (idA) A (idA),
Foreign key (idB) B (idB)
)
应用程序控制数据的方式是,在 表 AB 中,idA 和 idB 中的任何一个都会将另一个设置为 null。
如何强加这样的东西
Foreign key (idA) A (idA) ON DELETE CASCADE,
Foreign key (idB) B (idB) ON DELETE CASCADE,
或
Foreign key (idA) A (idA) ON DELETE CASCADE,
Foreign key (idB) B (idB) ON DELETE SET NULL,
或
Foreign key (idA) A (idA) ON DELETE SET NULL,
Foreign key (idB) B (idB) ON DELETE SET NULL,
在每种情况下我都会遇到错误:
SQL 错误 [1785] [S0000]:
在表 'inventory_exception' 上引入 FOREIGN KEY 约束 'FK__inventory_except__2022C2A6' 可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。
【问题讨论】:
-
您应该在查询之前使用 alter table x
-
保持外键不做任何操作,使用而不是删除触发器来删除子表中的数据。
-
为什么两个外键上的 ON DELETE SET NULL 都不起作用。即使我能够在没有触发的情况下做到这一点,我也会很好。
-
@atabrizi 我是直接在create table ddl中给的。
标签: sql sql-server