2016-10-06 24 views
0

我想truncate table tableA,但我必須禁用所有外鍵或放棄它們參照tableA之前,我這樣做。你能告訴我如何得到tableA有外鍵的表名嗎?如何獲取我的表有外鍵的表的列表?

+3

爲http:// stackoverflow.com/questions/1729996/list-of-foreign-keys-and-the-tables-theign-keys在這裏你應該找到答案 – Kacper

回答

1

好的,假設我想從SCOTT模式中刪除DEPT表。我檢查並發現它有一個名爲PK_DEPT的主鍵。然後我運行下面的查詢(對錶ALL_CONSTRAINTS)並查找引用此表的模式和表。

請記住所有的字符串值(表名,約束類型等)在所有的目錄表中都是大寫的。當你編寫WHERE條件時,這很重要。

select owner, table_name 
from all_constraints 
where constraint_type = 'R' and r_owner = 'SCOTT' and r_constraint_name = 'PK_DEPT'; 

OWNER    TABLE_NAME 
-------------------- -------------------- 
SCOTT    EMP 

1 row selected. 
1

一般的解決方案。

列出對錶外鍵約束所有表和所有者:表名是類型P(主鍵)或ü(唯一約束)

select owner, table_name, constraint_name 
    from all_constraints 
where r_constraint_name in (select constraint_name 
      from all_constraints 
       where table_name = :tablename 
       and constraint_type in ('P', 'U')); 
+0

更通用的解決方案是同時寫入針對ALL_CONSTRAINTS的select語句幷包含ref所有者以及TABLE_NAME,因爲參考約束可能跨越模式。 – mathguy

+0

正確mathguy,更改了sql以反映所有者並使用all_constraints –

相關問題