MYSQL query foreign key dependent_ and delete

1. Query which tables have added foreign key dependencies

SELECT C.TABLE_SCHEMA owner,
           C.REFERENCED_TABLE_NAME parent table name,
           C.REFERENCED_COLUMN_NAME parent table field,
           C.TABLE_NAME child table name,
           C.COLUMN_NAME subtable field,
           C.CONSTRAINT_NAME constraint name,
           T.TABLE_COMMENT table comment,
           R.UPDATE_RULE constraint update rule,
           R.DELETE_RULE constraint deletion rule
      FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE C
      JOIN INFORMATION_SCHEMA. TABLES T
        ON T.TABLE_NAME = C.TABLE_NAME
      JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS R
        ON R.TABLE_NAME = C.TABLE_NAME
       AND R.CONSTRAINT_NAME = C.CONSTRAINT_NAME
       AND R.REFERENCED_TABLE_NAME = C.REFERENCED_TABLE_NAME
      WHERE C.REFERENCED_TABLE_NAME IS NOT NULL 
            and C.TABLE_SCHEMA = 'database name'
            and C.REFERENCED_TABLE_NAME = 'Dependent table name';

2. Delete the foreign key

ALTER TABLE ${table name} DROP FOREIGN KEY ${foreign key name};

1) Delete one by one, the foreign key name may be different
2) If there are many, it is recommended to write a main method in java to generate sql

Guess you like

Origin blog.csdn.net/weixin_43460193/article/details/125082276