MySQL - How to see the foreign key relationships of a table?
You have a table and you want to see all the other tables which have the foreign key constraints pointing to that table, or to a particular column in that table.
To see foreign key relationships of a table:
SELECT
TABLE_NAME,
COLUMN_NAME,
CONSTRAINT_NAME,
REFERENCED_TABLE_NAME,
REFERENCED_COLUMN_NAME
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
REFERENCED_TABLE_SCHEMA = 'db_name'
AND REFERENCED_TABLE_NAME = 'table_name';
To see foreign key relationships of a column:
SELECT
TABLE_NAME,
COLUMN_NAME,
CONSTRAINT_NAME,
REFERENCED_TABLE_NAME,
REFERENCED_COLUMN_NAME
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
REFERENCED_TABLE_SCHEMA = 'db_name'
AND REFERENCED_TABLE_NAME = 'table_name'
AND REFERENCED_COLUMN_NAME = 'column_name';
You might not need to address the REFERENCED_TABLE_SCHEMA
for the current database you’re opening.
Need a good GUI Tool for MySQL? TablePlus is a modern, native tool with an elegant UI that allows you to simultaneously manage multiple databases such as MySQL, PostgreSQL, SQLite, Microsoft SQL Server and more.