Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query a Table's Foreign Key relationships

For a given table 'foo', I need a query to generate a set of tables that have foreign keys that point to foo. I'm using Oracle 10G.

like image 535
Mark Roddy Avatar asked Sep 17 '08 18:09

Mark Roddy


People also ask

How do I find a foreign key in a database?

select * from sys.foreign_keys If you want to search foreign keys created on a SQL table, you can use following SELECT command querying sys. foreign_keys system view on parent_object_id column value. On the other hand, if SQL programmer is dealing with SQL tables referring to a specific table, then sys.


1 Answers

This should work (or something close):

select table_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='<your table here>'); 
like image 45
Mike Monette Avatar answered Oct 19 '22 09:10

Mike Monette