List tables by foreign key in Mysql

Asked

Viewed 3,770 times

2

How to identify, by means of an instruction SQL, the tables that relate to another, that is, the tables that have a foreign key that references another table?

According to the dummy example, I need an instruction that shows how to output the list of tables linked to table product, in this case, the tables item_product and stockpile.

inserir a descrição da imagem aqui

1 answer

4


You can get the list of foreign keys by passing a table with this query:

SELECT
   constraint_name as nome_restricao,
   column_name as coluna_estrangeira,
   table_name as tabela_estrangeira,
   referenced_table_name as tabela_origem, 
   referenced_column_name as coluna_origem

FROM information_schema.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME = 'produto'

Mysql - KEY_COLUMN_USAGE

Mysql - Information_schema

Browser other questions tagged

You are not signed in. Login or sign up in order to post.