MySQL
keyboard_arrow_down 295 guides
chevron_leftKey Cookbooks
check_circle
Mark as learned thumb_up
1
thumb_down
0
chat_bubble_outline
0
Comment auto_stories Bi-column layout
settings
Searching all foreign keys to a table or column in MySQL
schedule Aug 11, 2023
Last updated local_offer
Tags MySQL
tocTable of Contents
expand_more Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!
Start your free 7-days trial now!
We can search for all foreign key constraints pointing to a particular table or a particular column in MySQL. The syntax varies slightly between searching for tables and searching for columns.
TO a table
To check all foreign keys that are referencing a particular table:
SELECT TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAMEFROM INFORMATION_SCHEMA.KEY_COLUMN_USAGEWHERE REFERENCED_TABLE_SCHEMA = 'database_name' AND REFERENCED_TABLE_NAME = 'table_name';
Where:
database_name
is the database within which the table you are interested in residestable_name
is the table you are interested in
TO a column
To check all foreign keys that are linking to a particular column:
SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAMEFROM INFORMATION_SCHEMA.KEY_COLUMN_USAGEWHERE REFERENCED_TABLE_SCHEMA = 'database_name' AND REFERENCED_TABLE_NAME = 'table_name' AND REFERENCED_COLUMN_NAME = 'column_name';
Where:
database_name
is the database within which the table you are interested in residestable_name
is the table within which the column you are interested in residescolumn_name
is the column you are interested in
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
1
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!