user_constraints,user_cons_columns查看外键

本文提供两种SQL查询语句,用于在Oracle数据库中查询指定表的主键和外键约束关系,包括主键拥有者、主键表、主键列、外键拥有者、外键表及外键列等详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
     


 

ORACLE数据库的系统参数都存储在数据库中,可以通过SQLPLUS,以用户SYSYTEM进行查询。几个重要的表或者视图如下: v$controlfile:控制文件的信息; v$datafile:数据文件的信息; v$log:日志文件的信息; v$process:处理器的信息; v$session:会话信息; v$transaction:事务信息; v$resource:资源信息; v$sga:系统全局区的信息。 上面的视图名中的‘v$’,只是视图名字中的字符。类似于上面的视图或表还有很多,位于: $ORACLE_HOME/RDBMS/ADMIN/CATALOG.SQL文件中。 这些视图或表可以在SQLPLUS中用SELECT语句进行查询。 2.数据字典视图 表和列 DBA_TABLES、ALL_TABLES和USER_TABLES显示了有关数据库表的一般信息。 DBA_TAB_COLUMNS、ALL_TAB_COLUMNSUSER_TAB_COLUMNS显示了每个数据库表的列的信息。 注意:DBA_OBJECTS、ALL_OBJECTS和USER_OBJECTS显示了模式对象的信息,包括表。 完整性约束 DBA_CONSTRAINTS、ALL_CONSTRAINTSUSER_CONSTRAINST显示有关约束的一般信息。 DBA_CONS_COLUMNS、ALL_CONS_COLUMNSUSER_CONS_COLUMNS显示有关列的相关约束的一般信息。 视图 DBA_VIEWS、ALL_VIEWS和USER_VIEWS。 注意:DBA_OBJECTS、ALL_OBJECTS和USER_OBJECTS显示了模式对象的信息,包括视图。 序列 DBA_SEQUENCES、ALL_SEQUENCES和USER_SEQUENCES。 注意:DBA_OBJECTS、ALL_OBJECTS和USER_OBJECTS显示了模式对象的信息,包括序列。 同义词 DBA_SYNONYMS、ALL_SYNONYMS和USER_SYNONYMS。 注意:DBA_OBJECTS、ALL_OBJECTS和USER_OBJECTS显示了模式对象的信息,包括同义词。 索引 DBA_INDEXS、ALL_INDEXS、USER_INDEXS、DBA_IND_COLUMNS、ALL_IND_COLUMNSUSER_IND_COLUMNS。 用户 DBA_USERS。 角色 DBA_ROLES。 表空间定额 DBA_TS_QUOTAS。 配置表 DBA_PROFILES。 表空间 DBA_TABLESPACES。 数据文件 DBA_DATA_FILES。 段 DBA_SEGMENTS、USER_SEGMENT。 回滚段 DBA_ROLLBACK_SEGS、V$ROLLNAME、V$ROLLSTAT。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值