[英]query to get constraint between parent and child table
I am new to oracle, I am working on tables where I need to fetch constraints between 2 tables and send the values as another input. 我是oracle新手,正在处理需要在2个表之间获取约束并将值作为另一个输入发送的表。
Table 1
|_ Column1_pk
|_ Column2 (foreign key to Table 2)
Table 2
|_column2
So I want to fetch primary key of table1 and column which have the relation between 2 tables 所以我想获取具有两个表之间关系的table1和column的主键
You need to query the dictionary views USER_CONSTRAINTS
and USER_CONS_COLUMNS
. 您需要查询字典视图
USER_CONSTRAINTS
和USER_CONS_COLUMNS
。 For example: 例如:
Table DEPT
has a primary key, and table EMP
has a foreign key pointing to DEPT
. 表
DEPT
有一个主键,表EMP
有一个指向DEPT
的外键。 First we find the constraint name, and then the column(s), of the primary key of DEPT
. 首先,我们找到约束名称,然后找到
DEPT
主键的列。 Then, with the constraint name in hand, we find the constraint name, and then the column(s), for the foreign key of EMP
. 然后,在拥有约束名称的情况下,我们找到约束名称,然后找到
EMP
外键的列。
select table_name, constraint_name, constraint_type
from user_constraints
where table_name = 'DEPT'
and constraint_type = 'P'
;
TABLE_NAME CONSTRAINT_NAME CONSTRAINT_TYPE
---------- --------------- ---------------
DEPT PK_DEPT P
select constraint_name, table_name, column_name, position
from user_cons_columns
where constraint_name = 'PK_DEPT'
;
CONSTRAINT_NAME TABLE_NAME COLUMN_NAME POSITION
--------------- ---------- ----------- --------
PK_DEPT DEPT DEPTNO 1
and then 接着
select table_name, constraint_name, constraint_type
from user_constraints
where table_name = 'EMP'
and r_constraint_name = 'PK_DEPT'
;
TABLE_NAME CONSTRAINT_NAME CONSTRAINT_TYPE
---------- --------------- ---------------
EMP FK_DEPTNO R
select constraint_name, table_name, column_name, position
from user_cons_columns
where constraint_name = 'FK_DEPTNO'
;
CONSTRAINT_NAME TABLE_NAME COLUMN_NAME POSITION
--------------- ---------- ----------- --------
FK_DEPTNO EMP DEPTNO 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.