If you use the information schema, you can easily construct the necessary ALTER TABLE
commands; the relevant tables are here: SCHEMA KEY_COLUMN_USAGE and STATISTICS.
Following is an example for a query which generates the DDL statements:
SELECT CONCAT('ALTER TABLE ',kcu.TABLE_NAME,' DROP FOREIGN KEY ', kcu.CONSTRAINT_NAME,';') AS ddl
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu, INFORMATION_SCHEMA.STATISTICS stat
WHERE stat.table_schema = 'your_schema'
AND kcu.TABLE_NAME = stat.TABLE_NAME
AND kcu.COLUMN_NAME = stat.COLUMN_NAME
AND kcu.REFERENCED_TABLE_NAME IS NOT NULL
INTO OUTFILE '/tmp/ddl.sql';
And you can run it after reviewing it by running this statement:
SOURCE '/tmp/ddl.sql';
Above statement will look up constraint name and corresponding table name for every foreign key constraint, where an index exists on the same foreign key column in the same table. The result is stored in the file given by INTO OUTFILE
.
Please review above statement carefully before running the generated ddl.