oracle 檢視主外來鍵約束
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
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
/////////////////////////////////////
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 |