For each instance of MySql
there is a database called information_schema
. This database contains all the information for all instance databases.
To recover all foreign keys, just make a SELECT
in the REFERENTIAL_CONSTRAINTS
table:
SELECT *
FROM information_schema.'REFERENTIAL_CONSTRAINTS';
If you want to filter the foreign keys of a specific table, add the WHERE
clause:
SELECT *
FROM information_schema.'REFERENTIAL_CONSTRAINTS' a
WHERE a.'CONSTRAINT_SCHEMA' = 'nome_do_banco' AND a.'TABLE_NAME' = 'nome_da_tabela'
There are many other useful things in this database, view the documentation .