Sunday, July 28, 2013

List Primary/Unique Keys and associated Foreign Keys

List the Primary/Unique Keys of a table.

select owner, constraint_name, constraint_type from dba_constraints where owner = 'OWNER' and table_name = 'TABLENAME' and constraint_type in ('P','U');

List the Foreign Keys based on the primary/unique keys of a parent table.

set linesize 200 select a.owner,a.table_name, a.constraint_name from dba_constraints a, (select owner,constraint_name from dba_constraints where owner = 'OWNER' and table_name = 'TABLENAME' and constraint_type in ('P','U')) b where a.constraint_type = 'R' and a.r_constraint_name = b.constraint_name and a.r_owner = b.owner;

******************************************
keywords: FK
******************************************
rdbms version: 11g
******************************************

No comments: