简体   繁体   English

在MySQL中无限制地强制执行删除级联

[英]Force a delete cascade with no constraint in MySQL

I'd like to know if it's possible to force a cascade when deleting in MySQL? 我想知道在MySQL中删除时是否可以强制级联?

I know that a in order to be able to delete all the childs of a parent this has to exists: 我知道,为了能够删除父级的所有子级,必须存在一个:

REFERENCES table (fk) ON DELETE CASCADE ON UPDATE CASCADE

But my tables doesn't have it, so, back to my question, is there a way to force it? 但是我的桌子上没有它,所以回到我的问题,有没有办法强迫它?

One either puts it in the original table creation, or attempts to establish it after the fact with ALTER TABLE 要么将其放入原始表创建中,要么尝试在事实发生后使用ALTER TABLE建立它

mysql Manual Page on ALTER TABLE Syntax mysql ALTER TABLE手册页语法

If one chooses to establish after the fact, the same rules apply as seen in the above link. 如果事实成立后选择建立,则适用相同的规则,如上面的链接所示。 Failing those or data pre-existing such that the enforcement will fail with the ALTER TABLE call, then the attempt will fail to establish the fk with a cascading delete. 如果这些或数据不存在,则执行ALTER TABLE调用将失败,则尝试将无法通过级联删除建立fk。

See that manual page, and look into ADD CONSTRAINT and DROP CONSTRAINT . 参见该手册页,并查看ADD CONSTRAINTDROP CONSTRAINT There is no altering of an existing constraint. 没有更改现有约束。 Rather one must drop it then re-add it. 而是必须丢弃它,然后重新添加它。

An alter add example" 另一个添加示例”

ALTER TABLE lunches
ADD CONSTRAINT `fk_lunches_users`
FOREIGN KEY (`studentId` )
REFERENCES `students` (`studentId` )
ON DELETE CASCADE;

Also of interest is foreign_key_checks that should be investigated. 还应该关注的是foreign_key_checks That is, is one willing to temporarily disable them. 也就是说,愿意暂时禁用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM