The query below lists all tables referenced by the foreign key for a specific table.
select distinct concat(table_schema, '.', table_name) as foreign_table, '>-' as rel, concat(referenced_table_schema, '.', referenced_table_name) as primary_table from information_schema.key_column_usage where referenced_table_name is not null and table_name = 'table name' -- provide table name here -- and table_schema = 'database name' order by primary_table;
- foreign_table - name of the foreign table - the table you provided as a parameter
- rel - relationship symbol implicating FK and direction
- primary_table - primary (referenced) tables names - the tables you are looking for
- One row: represents one referenced table
- Scope of rows: all tables referenced by the provided table name
- Ordered by: referencing table name
All tables referenced by FK for the store table in the sakila database (schema).