簡體   English   中英

如何找到現有表的約束?

[英]How to find constraints for existing table?

如何找到現有表的完整性約束(例如主鍵)? 我正在使用Oracle 10g。

查找表上的約束:

select constraint_name, constraint_type
from user_constraints
where table_name = 'YOUR_TABLE'

如果需要在其他模式中查找對表的約束,則有匹配的字典視圖ALL_和DBA_。

四種約束類型是

  • P主鍵
  • U獨特
  • R外鍵(參考)
  • C檢查

檢查約束包括我們不立即將其視為約束的內容,例如NOT NULL。

因此,要在表上查找主鍵...

select constraint_name, constraint_type
from user_constraints
where table_name = 'YOUR_TABLE'
and   c.constraint_type = 'P'

要查找外鍵中引用的表:

select c.constraint_name as foreign_key
       , c.r_constraint_name as referenced_constraint
       , p.table_name
from user_constraints c
     join user_constraints p
      on p.constraint_name  = c.r_constraint_name 
where c.table_name = 'YOUR_TABLE'
and   c.constraint_type = 'R'
/

您需要查詢數據字典,特別是USER_CONS_COLUMNS視圖,以查看表列和相應的約束:

SELECT *  FROM user_cons_columns WHERE table_name = '<your table name in caps>';

如果您要查找有關數據模型的信息,請使用為該作業構建的工具。

下載SQL Developer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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