您的位置:首页 > 大数据 > 人工智能

user_constraints,user_cons_columns查看外键

2009-05-26 13:38 239 查看
Oracle 查看一个表对应的主键和外键的约束关系,查看的语句:

select a.owner 主键拥有者

,a.table_name 主键表

,b.column_name 主键列

,C.OWNER 外键拥有者

,c.table_name 外键表

,d.column_name 外键列
from user_constraints a
left join user_cons_columns b

on a.constraint_name=b.constraint_name
left join user_constraints C

ON C.R_CONSTRAINT_NAME=a.constraint_name
left join user_cons_columns d

on c.constraint_name=d.constraint_name
where a.constraint_type='P'

and a.table_name='XXX' --需要查看主外键关系的表
order by a.table_name

宁外的一种写法

select
a.owner 外键拥有者,
a.table_name 外键表,
substr(c.column_name,1,127) 外键列,
b.owner 主键拥有者,
b.table_name 主键表,
substr(d.column_name,1,127) 主键列
from
user_constraints a,
user_constraints b,
user_cons_columns c,
user_cons_columns d
where
a.r_constraint_name=b.constraint_name
and a.constraint_type='R'
and b.constraint_type='P'
and a.r_owner=b.owner
and a.constraint_name=c.constraint_name
and b.constraint_name=d.constraint_name
and a.owner=c.owner
and a.table_name=c.table_name
and b.owner=d.owner
and b.table_name=d.table_name

数据字典表列说明:

desc user_constraints

NameComments
OWNER表的所有者
CONSTRAINT_NAME约束名
CONSTRAINT_TYPE约束类型
TABLE_NAMEName associated with table with constraint definition
SEARCH_CONDITIONText of search condition for table check
R_OWNEROwner of table used in referential constraint
R_CONSTRAINT_NAMEName of unique constraint definition for referenced table
DELETE_RULEThe delete rule for a referential constraint
STATUSEnforcement status of constraint - ENABLED or DISABLED
DEFERRABLEIs the constraint deferrable - DEFERRABLE or NOT DEFERRABLE
DEFERREDIs the constraint deferred by default - DEFERRED or IMMEDIATE
VALIDATEDWas this constraint system validated? - VALIDATED or NOT VALIDATED
GENERATEDWas the constraint name system generated? - GENERATED NAME or USERNAME
BADCreating this constraint should give ORA-02436. Rewrite it before2000 AD.
RELYIf set, this flag will be used in optimizer
LAST_CHANGEThe date when this column was last enabled or disabled
INDEX_OWNERThe owner of the index used by the constraint
INDEX_NAMEThe index used by the constraint
INVALID
VIEW_RELATED
desc user_cons_columns;

NameDefault Comments
OWNEROwner of the constraint definition
CONSTRAINT_NAMEName associated with the constraint definition
TABLE_NAMEName associated with table with constraint definition
COLUMN_NAMEName associated with column or attribute of object column specified in the constraint definition
POSITIONOriginal position of column or attribute in definition
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: