Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See all the tables that have foreign keys to a certain column in a table?

Is this possible ? If yes, what are the tables involved, somewhere to look into .

like image 783
Andrei Ciobanu Avatar asked May 27 '11 12:05

Andrei Ciobanu


People also ask

How do you find all tables that have foreign keys that reference particular table column?

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';


1 Answers

Try something like this

select uc.table_name 
from sys.user_cons_columns ucc
join sys.user_constraints uc 
on uc.r_constraint_name = ucc.constraint_name
where constraint_type = 'R'
and ucc.table_name = :1
and ucc.column_name = :2

where

:1 = referenced table
:2 = referenced column
like image 92
Lukas Eder Avatar answered Oct 19 '22 20:10

Lukas Eder