Home >Database >Mysql Tutorial >How to View Foreign Key Constraints in MySQL?
Querying foreign key constraints in MySQL can provide valuable insights into data associations. This guide addresses the question of how to retrieve a comprehensive list of foreign key constraints referencing a specific table or column.
To list all foreign key constraints pointing to a particular table, execute the following query:
SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = (SELECT DATABASE()) AND REFERENCED_TABLE_NAME = '<table name>' \G
To focus on foreign keys referencing a specific column within a table, modify the query as follows:
SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = (SELECT DATABASE()) AND REFERENCED_TABLE_NAME = '<table name>' AND REFERENCED_COLUMN_NAME = '<column name>' \G
In both queries, replace