Monday, April 25, 2011

How to get references to a table

In Oracle, if you need to know which tables have a DIRECT relationship with a given table, not like a table that has a foreign key of a second table that in turn has a foreign key pointing to a third table, then you can use this SQL sentence:

SQL> select owner, constraint_name, constraint_type, 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 owner='MYUSER' and table_name='MYTABLE');

OWNER CONSTRAINT_NAME C TABLE_NAME
-------------------- -------------------- - ---------------------
ANOWNER SYS_C0018146 R ATABLE
ANOWNER SYS_C0018144 R OTHER_TABLE
ANOWNER SYS_C0018145 R A_THIRD_TABLE
ANOWNER SYS_C0018148 R RELATED_TABLE

No comments:

Post a Comment