简体   繁体   English

以编程方式将ON DELETE CASCADE添加到所有表上的现有外键约束中

[英]Programmatically add ON DELETE CASCADE to existing foreign key constraints on all tables

I'm working with a large database (over 250 tables) that has many foreign keys, but none are set to ON DELETE CASCADE. 我正在使用具有许多外键的大型数据库(超过250个表),但是没有一个设置为ON DELETE CASCADE。 I realize that this means I need to drop the foreign key constraint and recreate it. 我意识到这意味着我需要删除外键约束并重新创建它。 That in and of itself is not too bad. 这本身并不算太糟。 But at 874 existing foreign key constraints, that could really take me a long time. 但是在现有的874个外键约束条件下,这确实可能要花我很长时间。

Does anyone have any suggestions on how to automate this? 有人对如何实现此自动化有任何建议吗? I'm a PHP developer by trade, but I'm open to any solution in any language that anyone can think of. 我是从事贸易的PHP开发人员,但是我愿意接受任何人都能想到的任何语言的任何解决方案。

I think you can do it programmatically. 我认为您可以以编程方式进行操作。 When a main entity needs to be deleted. 需要删除主要实体时。 Loop thought all the rows in all tables that has this entity's PK as FK. Loop将所有具有该实体PK的表中的所有行都视为FK。 Delete them first. 首先删除它们。 Then delete the main row(entity) in the main table. 然后删除主表中的主行(实体)。 Do this in a transaction. 在事务中执行此操作。 I don't know. 我不知道。 Both ways sound too much of a mess. 两种方法听起来都太混乱了。

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

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