Is there any command to list all foreign keys in a MYSQL table?

1

I would like to know how to list all foreign keys in a specific table.

For example:

 usuarios [id, nome, nivel_id*, empresa_id*, cargo_id*]

In the above case, you would list the fields empresa_id , nivel_id and cargo_id with their respective information.

    
asked by anonymous 28.08.2018 / 19:09

1 answer

2

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 .

    
28.08.2018 / 19:16