簡體   English   中英

查詢以獲取父子表之間的約束

[英]query to get constraint between parent and child table

我是oracle新手,正在處理需要在2個表之間獲取約束並將值作為另一個輸入發送的表。

Table 1
  |_ Column1_pk
  |_ Column2 (foreign key to Table 2) 

Table 2
  |_column2 

所以我想獲取具有兩個表之間關系的table1和column的主鍵

您需要查詢字典視圖USER_CONSTRAINTSUSER_CONS_COLUMNS 例如:

DEPT有一個主鍵,表EMP有一個指向DEPT的外鍵。 首先,我們找到約束名稱,然后找到DEPT主鍵的列。 然后,在擁有約束名稱的情況下,我們找到約束名稱,然后找到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

接着

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM