Marcin Kossakowski

Marcin Kossakowski

Tech and stuff

07 Apr 2017

Finding Which Tables Reference Column in Given Table in MySQL

When dealing with schema updates in MySQL I often have to check what foreign key constraints are in place. How can we find which table have columns that reference the primary column on the table we’re modifying. Here’s the SQL statement that will help us find those references.

SELECT
TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
REFERENCED_TABLE_SCHEMA = 'database_name' AND
REFERENCED_TABLE_NAME = 'table_name';