最好使用
CASCADE
用于多个操作,即删除或更新。
级联
UPDATE CASCADE
或
ON UPDATE SET NULL
递归以更新之前在级联过程中更新的相同表
RESTRICT
这意味着您不能在更新级联或上使用自引用
UPDATE SET NULL
操作。这是为了防止级联更新产生无限循环。自我参照
ON DELETE SET NULL
另一方面,它是可能的,因为它是自指的
ON DELETE CASCADE
级联操作的嵌套深度不得超过15层。
有时,了解哪个表受
MySQL ON DELETE CASCADE
从父表中删除数据时的引用操作。您可以从information_schema数据库中的referential_constraints查询此数据,如下所示:
代码:
USE information_schema;
SELECT table_name
FROM referential_constraints
WHERE constraint_schema = 'database_name' AND
referenced_table_name = 'parent_table' AND
delete_rule = 'CASCADE'
例子:
例如,要查找与
建筑
与
CASCADE delete rule
在
数据库中,您可以使用以下查询:
USE information_schema;
SELECT table_name
FROM referential_constraints
WHERE constraint_schema = 'classicmodels' AND
referenced_table_name = 'buildings' AND
delete_rule = 'CASCADE'
另一个替代示例:
DELETE CASCADE
:
如果您的
cascading deletes
如果因为某个产品是被杀类别的成员而对其进行核弹攻击,那么您的外键设置不正确。给定示例表,您应该具有以下表设置:
CREATE TABLE category (
id int unsigned not null primary key,
name VARCHAR(255) default null
)Engine=InnoDB;
CREATE TABLE product (
id int unsigned not null primary key,
name VARCHAR(255) default null
)Engine=InnoDB;
CREATE TABLE category_product (
category_id int unsigned not null,
product_id int unsigned not null,
PRIMARY KEY (category_id, product_id),
KEY pkey (product_id),
FOREIGN KEY (category_id) REFERENCES category (id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (product_id) REFERENCES product (id)
ON DELETE CASCADE
ON UPDATE CASCADE
)Engine=InnoDB;