Delete all foreign keys in database(MySql)

前端 未结 5 704
广开言路
广开言路 2020-12-15 18:04

I would like to rename a column in a table that is a foreign key to many tables. Apparently this is only possible if you delete the constraints, as I found out in this link.

5条回答
  •  粉色の甜心
    2020-12-15 18:34

    I was still having some issues after finding this thread, but I have seemed to find a workflow that works for me.

    Database Deletion

    If you are using MySQL Workbench, you will need to turn SAFE UPDATE off by doing the following 3 steps:

    1. MySql Workbench > Preferences > SQL EDITOR > (bottom of settings)
    2. turn the SAFE UPDATE off
    3. then log out of your connection and reconnect

    Steps to start purge database and start new

    1. Delete all data from tables

    (This MySQL script will create a DELETE FROM script for each table in your database.)

    SELECT concat('DELETE FROM ',table_schema,'.',table_name,';') FROM information_schema.table_constraints WHERE table_schema='!!TABLE_SCHEMA!!';

    Copy the output of this and run it. You might need this line-by-line.

    1. Delete all Foreign Keys from your tables

    (This MySql script will create na ALTER TABLE _ DROP FOREIGN KEY script for each table in your database.)

    SELECT concat('alter table ',table_schema,'.',table_name,' DROP FOREIGN KEY ',constraint_name,';') FROM information_schema.table_constraints WHERE constraint_type='FOREIGN KEY' AND table_schema='!!TABLE_SCHEMA!!';

    Copy the output of this and run it. You might need this line-by-line.

    After you've done this, you SHOULD be able to run your DROP DATABASE script.

提交回复
热议问题