Is there any way to get all tables that have foreign keys to another table in oracle with a query?
+4
A:
Here is a good article with an answer:
select owner,constraint_name,constraint_type,table_name,r_owner,r_constraint_name
from all_constraints
where constraint_type='R'
and r_constraint_name in (select constraint_name from all_constraints
where constraint_type in ('P','U') and table_name='TABLE_NAME');
crb
2009-04-08 19:20:44
I'll check that view - the query returns the table that has the FK, and I'd need to specify one table and get every other table that has a FK to it
Juan Manuel
2009-04-08 19:27:41
I edited it with the correct query from the article, so I could accept it
Juan Manuel
2009-04-08 19:34:04
+1
A:
Assuming that both the parent and child tables are in the same schema:
select t1.table_name child_table, t1.constraint_name, t2.table_name parent_table
from user_constraints t1, user_constraints t2
where t1.r_constraint_name = t2.constraint_name
Note that r_constraint_name is populated only for FK (type 'R') constraints, so the self-join only returns info of interest
dpbradley
2009-04-08 19:56:24