简体   繁体   中英

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. 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.

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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