You Asked
I am interested in finding out the constraints on table and view columns (eg, if they are primary keys, foreign keys and what table/columns are referenced, any default values, if they are constants, any check constraints for the column, and if they reference a v. 8.0 nested column). Though the dictionary views USER_TABLES, _TAB_COLUMNS AND _CONS_COLUMNS give some information on column constraints (eg, if null, data type and size), I could not find any views for the particular information I needed and have listed above. Could you tell me where I might find this information ?
Thanks, Jack
and Tom said...
1) primary keys on a table:
scott@TKYTE816> select a.constraint_name, b.column_name
2 from user_constraints a, user_cons_columns b
3 where a.constraint_type = 'P' and a.constraint_name = b.constraint_name
4 and a.table_name = 'EMP'
5 order by b.position
6 /
CONSTRAINT_NAME COLUMN_NAME
------------------------------ ------------------------------
EMP_PK EMPNO
scott@TKYTE816> 4
4* and a.table_name = 'EMP'
scott@TKYTE816> c/EMP/DEPT
4* and a.table_name = 'DEPT'
scott@TKYTE816> /
CONSTRAINT_NAME COLUMN_NAME
------------------------------ ------------------------------
DEPT_PK DEPTNO
2) foreign keys -- this query is overkill for you but I had it lying around. It generates the foreign key statements given a parent table (i use it to drop and rebuild constraints for a truncate or something like that)
scott@TKYTE816> @cons emp
scott@TKYTE816> column fkey format a80 word_wrapped
scott@TKYTE816> select
2 'alter table "' || child_tname || '"' || chr(10) ||
3 'add constraint "' || child_cons_name || '"' || chr(10) ||
4 'foreign key ( ' || child_columns || ' ) ' || chr(10) ||
5 'references "' || parent_tname || '" ( ' || parent_columns || ');' fkey
6 from
7 ( select a.table_name child_tname, a.constraint_name child_cons_name,
8 b.r_constraint_name parent_cons_name,
9 max(decode(position, 1, '"'||
10 substr(column_name,1,30)||'"',NULL)) ||
11 max(decode(position, 2,', '||'"'||
12 substr(column_name,1,30)||'"',NULL)) ||
13 max(decode(position, 3,', '||'"'||
14 substr(column_name,1,30)||'"',NULL)) ||
15 max(decode(position, 4,', '||'"'||
16 substr(column_name,1,30)||'"',NULL)) ||
17 max(decode(position, 5,', '||'"'||
18 substr(column_name,1,30)||'"',NULL)) ||
19 max(decode(position, 6,', '||'"'||
20 substr(column_name,1,30)||'"',NULL)) ||
21 max(decode(position, 7,', '||'"'||
22 substr(column_name,1,30)||'"',NULL)) ||
23 max(decode(position, 8,', '||'"'||
24 substr(column_name,1,30)||'"',NULL)) ||
25 max(decode(position, 9,', '||'"'||
26 substr(column_name,1,30)||'"',NULL)) ||
27 max(decode(position,10,', '||'"'||
28 substr(column_name,1,30)||'"',NULL)) ||
29 max(decode(position,11,', '||'"'||
30 substr(column_name,1,30)||'"',NULL)) ||
31 max(decode(position,12,', '||'"'||
32 substr(column_name,1,30)||'"',NULL)) ||
33 max(decode(position,13,', '||'"'||
34 substr(column_name,1,30)||'"',NULL)) ||
35 max(decode(position,14,', '||'"'||
36 substr(column_name,1,30)||'"',NULL)) ||
37 max(decode(position,15,', '||'"'||
38 substr(column_name,1,30)||'"',NULL)) ||
39 max(decode(position,16,', '||'"'||
40 substr(column_name,1,30)||'"',NULL))
41 child_columns
42 from user_cons_columns a, user_constraints b
43 where a.constraint_name = b.constraint_name
44 and b.constraint_type = 'R'
45 group by a.table_name, a.constraint_name, b.r_constraint_name ) child,
46 ( select a.constraint_name parent_cons_name, a.table_name parent_tname,
47 max(decode(position, 1, '"'||
48 substr(column_name,1,30)||'"',NULL)) ||
49 max(decode(position, 2,', '||'"'||
50 substr(column_name,1,30)||'"',NULL)) ||
51 max(decode(position, 3,', '||'"'||
52 substr(column_name,1,30)||'"',NULL)) ||
53 max(decode(position, 4,', '||'"'||
54 substr(column_name,1,30)||'"',NULL)) ||
55 max(decode(position, 5,', '||'"'||
56 substr(column_name,1,30)||'"',NULL)) ||
57 max(decode(position, 6,', '||'"'||
58 substr(column_name,1,30)||'"',NULL)) ||
59 max(decode(position, 7,', '||'"'||
60 substr(column_name,1,30)||'"',NULL)) ||
61 max(decode(position, 8,', '||'"'||
62 substr(column_name,1,30)||'"',NULL)) ||
63 max(decode(position, 9,', '||'"'||
64 substr(column_name,1,30)||'"',NULL)) ||
65 max(decode(position,10,', '||'"'||
66 substr(column_name,1,30)||'"',NULL)) ||
67 max(decode(position,11,', '||'"'||
68 substr(column_name,1,30)||'"',NULL)) ||
69 max(decode(position,12,', '||'"'||
70 substr(column_name,1,30)||'"',NULL)) ||
71 max(decode(position,13,', '||'"'||
72 substr(column_name,1,30)||'"',NULL)) ||
73 max(decode(position,14,', '||'"'||
74 substr(column_name,1,30)||'"',NULL)) ||
75 max(decode(position,15,', '||'"'||
76 substr(column_name,1,30)||'"',NULL)) ||
77 max(decode(position,16,', '||'"'||
78 substr(column_name,1,30)||'"',NULL))
79 parent_columns
80 from user_cons_columns a, user_constraints b
81 where a.constraint_name = b.constraint_name
82 and b.constraint_type in ( 'P', 'U' )
83 group by a.table_name, a.constraint_name ) parent
84 where child.parent_cons_name = parent.parent_cons_name
85 and parent.parent_tname = upper('&1')
86 /
old 85: and parent.parent_tname = upper('&1')
new 85: and parent.parent_tname = upper('emp')
FKEY
--------------------------------------------------------------------------------
alter table "EMP"
add constraint "EMP_FK_EMP"
foreign key ( "MGR" )
references "EMP" ( "EMPNO");
3) default values:
scott@TKYTE816> select column_name, data_default
2 from user_tab_columns
3 where table_name = 'EMP'
4 and data_default is not null;
no rows selected
scott@TKYTE816> alter table emp modify sal default 100;
Table altered.
scott@TKYTE816> select column_name, data_default
2 from user_tab_columns
3 where table_name = 'EMP'
4 and data_default is not null;
COLUMN_NAME
------------------------------
DATA_DEFAULT
------------------------------------------------------------
SAL
100
4) constraints:
scott@TKYTE816> select constraint_name, constraint_type, search_condition
2 from user_constraints
3 where table_name = 'EMP'
4 /
CONSTRAINT_NAME C SEARCH_CONDITION
------------------------------ - ------------------------------
SYS_C003382 C "EMPNO" IS NOT NULL
EMP_PK P
EMP_FK_DEPT R
EMP_FK_EMP R
SAL_CHK C sal < 10000
you could add where constraint_type = 'C' just to get check constraints.
Rating
(6 ratings)
Is this answer out of date? If it is, please let us know via a Comment