| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Usenet -> c.d.o.server -> Re: *** Table Constraints ***
/*
Reporting Table Constraints
*/
/*
This script reports most of the constraints against a given table.
If the constraint is a primary key, it will show all columns involved and all
tables that reference it through foreign key constraints.
If the constraint is a foreign key, it will show all columns involved and the
table it references.
Otherwise it will describe the rule of the constraint.
*/
REM Script:
REM tbconst.sql
REM
REM
REM NOTE * Need select access against sys.dba_cons_columns and REM sys.dba_constraints to run this.REM
REM owner = owner of the table REM table = name of table to report onREM
REM ---------- ----------------- ----------------------------------------REM Capture owner and table name parameters def owner = &owner
create or replace package body pdcconstraint as
function pdc$getcols ( cn_name in varchar2, cn_owner in varchar2, tb_name in
varchar2)
return varchar2
as
val varchar2(500);
col_name varchar2(30);
found boolean;
cursor c1 is
select column_name
from sys.dba_cons_columns
where constraint_name = upper(cn_name)
and owner = upper(cn_owner)
and table_name = upper(tb_name);
Begin
val := '';
found := FALSE;
for record in c1
Loop
if found = FALSE then
found := TRUE;
else val := val ||', ';
end if;
val := val || record.column_name;
end loop;
function pdc$getdesc ( cn_name in varchar2, cn_owner in varchar2, tb_name in
varchar2) return varchar2
as cn_type char(1);
descr varchar2(2000);
found boolean;
cursor c1 is
select owner || '.' || table_name val
from sys.dba_constraints
where r_owner = upper(cn_owner)
and r_constraint_name = upper(cn_name);
begin
found := FALSE;
descr := '';
select constraint_type
into cn_type
from sys.dba_constraints
where constraint_name = upper(cn_name)
and owner = upper(cn_owner)
and table_name = upper(tb_name);
if cn_type = 'U' then
descr := ' ';
else if cn_type = 'P' then
descr := 'Referenced by: ';
for record in c1 loop
if found = FALSE then
found := TRUE;
else descr := descr || ', ';
end if;
descr := descr || record.val;
end loop;
else if cn_type = 'R' then
select 'References ' || b.owner || '.' || b.table_name
into descr
from dba_constraints a, dba_constraints b
where a.table_name = upper(tb_name)
and a.owner = upper(cn_owner)
and a.constraint_name = upper(cn_name)
and b.owner = a.r_owner
and b.constraint_name = a.r_constraint_name;
else if cn_type = 'C' then
select search_condition
into descr
from dba_constraints
where constraint_name = upper(cn_name)
and owner = upper(cn_owner)
and table_name = upper(tb_name);
descr := ltrim(descr);
else descr := ' ';
end if;
end if;
end if;
end if;
return descr;
select constraint_name,
'Primary Key' type,
pdcconstraint.pdc$getcols(constraint_name,'&owner','&table_name')
cols, pdcconstraint.pdc$getdesc(constraint_name,'&owner','&table_name')
des
select constraint_name,
'Referential' type,
pdcconstraint.pdc$getcols(constraint_name,'&owner','&table_name')
cols,
pdcconstraint.pdc$getdesc(constraint_name,'&owner','&table_name')
des
from dba_constraints
where owner=upper('&owner')
and table_name =upper('&table_name')
and constraint_type = 'R'
union
select constraint_name,
'Table Check' type,
pdcconstraint.pdc$getcols(constraint_name,'&owner','&table_name')
cols,
pdcconstraint.pdc$getdesc(constraint_name,'&owner','&table_name')
des
from dba_constraints
where owner=upper('&owner')
and table_name = upper('&table_name')
and constraint_type = 'C'
union
select constraint_name,
'View Check' type,
pdcconstraint.pdc$getcols(constraint_name,'&owner','&table_name')
cols,
pdcconstraint.pdc$getdesc(constraint_name,'&owner','&table_name')
des
from dba_constraints
where owner=upper('&owner')
and table_name = upper('&table_name')
and constraint_type = 'V'
union
select constraint_name, 'Unique' type,
pdcconstraint.pdc$getcols(constraint_name,'&owner','&table_name') cols,
pdcconstraint.pdc$getdesc(constraint_name,'&owner','&table_name') des
from dba_constraints
where owner=upper('&owner')
and table_name = upper('&table_name')
and constraint_type = 'U'
order by 2,1
/
drop package pdcconstraint;
REM Clear variables
undefine owner
undefine table_name
undefine gScript
undefine gTitle
ttitle off
btitle off
clear column
clear breaks
REM End of Script
Paul in VT Received on Thu Aug 26 1999 - 15:43:52 CDT
![]() |
![]() |