Question

I need to find and fix all the onDelete clauses of foreign keys, on a MySQL database, because all of them are now by default RESTRICT.

Can this be done by a MySQL-query?

At least to find all the foreign keys

UPDATE after @peterm answer:

SELECT * FROM information_schema.`REFERENTIAL_CONSTRAINTS` 
WHERE CONSTRAINT_SCHEMA = '%database_name%' AND delete_rule = 'RESTRICT';
Was it helpful?

Solution

You can use INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS

The REFERENTIAL_CONSTRAINTS table provides information about foreign keys.

DELETE_RULE field is what you're looking for.

OTHER TIPS

To find all Foreign Keys in a database table, you may want to attempt the following SQL query:

use INFORMATION_SCHEMA;

select TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,
REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME from KEY_COLUMN_USAGE where
REFERENCED_TABLE_NAME = '<mytable>';

Or you could also try executing the following query to show how the table was created:

SHOW CREATE TABLE <mytable>;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top