oracle 查看主外键约束

select a.constraint_name, a.table_name, b.constraint_name 
from user_constraints a, user_constraints b
where a.constraint_type = 'R' 
and b.constraint_type = 'P' 
and a.r_constraint_name = b.constraint_name

P 代表主键
R 代表外键

通过关联,能查询到你所想要的一切。

 

select constraint_name from dba_cons_columns where table_name='';
Alter table XX drop constraint sys…

/

user_constraints,user_cons_columns查看外键

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

Name Comments
OWNER 表的所有者
CONSTRAINT_NAME 约束名
CONSTRAINT_TYPE 约束类型
TABLE_NAME Name associated with table with constraint definition
SEARCH_CONDITION Text of search condition for table check
R_OWNER Owner of table used in referential constraint
R_CONSTRAINT_NAME Name of unique constraint definition for referenced table
DELETE_RULE The delete rule for a referential constraint
STATUS Enforcement status of constraint - ENABLED or DISABLED
DEFERRABLE Is the constraint deferrable - DEFERRABLE or NOT DEFERRABLE
DEFERRED Is the constraint deferred by default - DEFERRED or IMMEDIATE
VALIDATED Was this constraint system validated? - VALIDATED or NOT VALIDATED
GENERATED Was the constraint name system generated? - GENERATED NAME or USERNAME
BAD Creating this constraint should give ORA-02436. Rewrite it before2000 AD.
RELY If set, this flag will be used in optimizer
LAST_CHANGE The date when this column was last enabled or disabled
INDEX_OWNER The owner of the index used by the constraint
INDEX_NAME The index used by the constraint
INVALID  
VIEW_RELATED  


desc user_cons_columns;

Name Default Comments
OWNER Owner of the constraint definition
CONSTRAINT_NAME Name associated with the constraint definition
TABLE_NAME Name associated with table with constraint definition
COLUMN_NAME Name associated with column or attribute of object column specified in the constraint definition
POSITION Original position of column or attribute in definition

 

http://blog.csdn.net/maqinqin/archive/2009/05/26/4217045.aspx

desc   后发现
user_constraints是表约束的视图,描述的是约束类型(constraint_type)是什么,属于哪些表(table_name),如 果约束的类型为R(外键)的话,那么r_constraint_name字段存放的就是被引用主表中的主键约束名。   
user_cons_columns是表约束字段的视图,说明表中的和约束相关的列参与了哪些约束。这些约束有主键约束,外键约束,索引约束.
两者可以通过(owner,constraint_name,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

Name                                      

http://www.blogjava.net/sunzhong/articles/289139.html

 

/

 

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

 

Name Comments
OWNER 表的所有者
CONSTRAINT_NAME 约束名
CONSTRAINT_TYPE 约束类型
TABLE_NAME Name associated with table with constraint definition
SEARCH_CONDITION Text of search condition for table check
R_OWNER Owner of table used in referential constraint
R_CONSTRAINT_NAME Name of unique constraint definition for referenced table
DELETE_RULE The delete rule for a referential constraint
STATUS Enforcement status of constraint -  ENABLED or DISABLED
DEFERRABLE Is the constraint deferrable - DEFERRABLE or NOT DEFERRABLE
DEFERRED Is the constraint deferred by default -  DEFERRED or IMMEDIATE
VALIDATED Was this constraint system validated? -  VALIDATED or NOT VALIDATED
GENERATED Was the constraint name system generated? -  GENERATED NAME or USERNAME
BAD Creating this constraint should give ORA-02436.  Rewrite it before2000 AD.
RELY If set, this flag will be used in optimizer
LAST_CHANGE The date when this column was last enabled or disabled
INDEX_OWNER The owner of the index used by the constraint
INDEX_NAME The index used by the constraint
INVALID  
VIEW_RELATED  

desc user_cons_columns;

 

Name Default Comments
OWNER Owner of the constraint definition
CONSTRAINT_NAME Name associated with the constraint definition
TABLE_NAME Name associated with table with constraint definition
COLUMN_NAME Name associated with column or attribute of object column specified in the constraint definition
POSITION Original position of column or attribute in definition
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值