2016-10-06 27 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 –

相关问题