The following scripts displays the primary key and foreign key constraints for a given table.
COLUMN CONSTRAINT_NAME FORMAT A25
COLUMN COLUMNS FORMAT A40
COLUMN TABLE_NAME FORMAT A40
SET LINESIZE 500
DEFINE TAB = &TABLENAME
select constraint_name, DECODE(constraint_type,'P','PRIMARY KEY','R','FOREIGN KEY',CONSTRAINT_TYPE)
CONSTRAINT_TYPE,
rtrim(
max(decode(position,1,column_name,null))
|| ','||
max(decode(position,2,column_name,null))
|| ','||
max(decode(position,3,column_name,null)),
',' ) columns, table_name
from (
select a.constraint_name,
a.constraint_type,
b.column_name,
b.position,
b.table_name
from ALL_constraints a,
ALL_cons_columns b
where
(( a.constraint_type = 'P' and
a.table_name = UPPER('&&TAB' ))
or
( a.constraint_type = 'R' and
a.r_constraint_name =
( select
constraint_name
from
ALL_constraints
where
constraint_type = 'P'
and
table_name = UPPER('&&TAB' ))
) )
and a.constraint_name =
b.constraint_name
)
group by
constraint_name, constraint_type, TABLE_NAME
order by 2,1
undefine tablename
/
SET LINESIZE 100