在Oracle SQL Developer中,如果我正在查看表中的信息,我可以查看约束,这可以让我看到外键(以及这个表由哪个表引用),我可以查看依赖关系,看看哪些包和这样的引用表。但我不知道如何找到哪些表引用该表。
例如,假设我正在查看emp
表。还有另一个表emp_dept
,用于捕获哪些员工在哪些部门工作,这些部门引用emp
表到emp_id
表,emp
表的主键。有没有办法(通过程序中的某个UI元素,而不是通过SQL)找到emp_dept
表引用emp
表,而不必知道emp_dept
表存在?
答案 0 :(得分:230)
没有。 Oracle SQL Developer没有这样的选项。
您必须手动执行查询或使用其他工具(例如PLSQL Developer有此选项)。以下SQL是PLSQL Developer使用的SQL:
select table_name, constraint_name, status, owner
from all_constraints
where r_owner = :r_owner
and constraint_type = 'R'
and r_constraint_name in
(
select constraint_name from all_constraints
where constraint_type in ('P', 'U')
and table_name = :r_table_name
and owner = :r_owner
)
order by table_name, constraint_name
其中r_owner
是架构,r_table_name
是您要查找引用的表。名称区分大小写
要小心,因为在Oracle SQL Developer的“报告”选项卡上有“所有表/依赖关系”选项,它来自ALL_DEPENDENCIES,它指的是“过程,包,函数,包体之间的依赖关系,当前用户可以访问的触发器,包括对没有任何数据库链接的视图的依赖。“。然后,此报告对您的问题没有任何价值。
答案 1 :(得分:100)
要将此作为扩展添加到SQL Developer,请执行以下操作:
<items>
<item type="editor" node="TableNode" vertical="true">
<title><![CDATA[FK References]]></title>
<query>
<sql>
<![CDATA[select a.owner,
a.table_name,
a.constraint_name,
a.status
from all_constraints a
where a.constraint_type = 'R'
and exists(
select 1
from all_constraints
where constraint_name=a.r_constraint_name
and constraint_type in ('P', 'U')
and table_name = :OBJECT_NAME
and owner = :OBJECT_OWNER)
order by table_name, constraint_name]]>
</sql>
</query>
</item>
</items>
将扩展添加到SQL Developer:
导航到任何表,您现在应该在SQL 1旁边看到一个附加选项卡,标记为FK References,它显示新的FK信息。
参考
答案 2 :(得分:33)
在下面的查询中将[你的桌子]替换为 emp
select owner,constraint_name,constraint_type,table_name,r_owner,r_constraint_name
from all_constraints
where constraint_type='R'
and r_constraint_name in (select constraint_name
from all_constraints
where constraint_type in ('P','U')
and table_name='[YOUR TABLE]');
答案 3 :(得分:8)
您可以从ALL_CONSTRAINTS
视图中查询:
SELECT table_name
FROM ALL_CONSTRAINTS
WHERE constraint_type = 'R' -- "Referential integrity"
AND r_constraint_name IN
( SELECT constraint_name
FROM ALL_CONSTRAINTS
WHERE table_name = 'EMP'
AND constraint_type IN ('U', 'P') -- "Unique" or "Primary key"
);
答案 4 :(得分:8)
2015年5月发布的SQL Developer 4.1添加了一个Model选项卡,显示了以实体关系图格式引用表的表外键。
答案 5 :(得分:4)
这样的事情怎么样:
SELECT c.constraint_name, c.constraint_type, c2.constraint_name, c2.constraint_type, c2.table_name
FROM dba_constraints c JOIN dba_constraints c2 ON (c.r_constraint_name = c2.constraint_name)
WHERE c.table_name = <TABLE_OF_INTEREST>
AND c.constraint_TYPE = 'R';
答案 6 :(得分:4)
SELECT DISTINCT table_name,
constraint_name,
column_name,
r_table_name,
position,
constraint_type
FROM (SELECT uc.table_name,
uc.constraint_name,
cols.column_name,
(SELECT table_name
FROM user_constraints
WHERE constraint_name = uc.r_constraint_name) r_table_name,
(SELECT column_name
FROM user_cons_columns
WHERE constraint_name = uc.r_constraint_name
AND position = cols.position) r_column_name,
cols.position,
uc.constraint_type
FROM user_constraints uc
inner join user_cons_columns cols
ON uc.constraint_name = cols.constraint_name
WHERE constraint_type != 'C')
START WITH table_name = '&&tableName'
AND column_name = '&&columnName'
CONNECT BY NOCYCLE PRIOR table_name = r_table_name
AND PRIOR column_name = r_column_name;
答案 7 :(得分:4)
答案 8 :(得分:0)
要添加到上述针对sql developer插件的答案,请使用以下xml有助于获取与外键关联的列。
<items>
<item type="editor" node="TableNode" vertical="true">
<title><![CDATA[FK References]]></title>
<query>
<sql>
<![CDATA[select a.owner,
a.constraint_name,
a.table_name,
b.column_name,
a.status
from all_constraints a
join all_cons_columns b ON b.constraint_name = a.constraint_name
where a.constraint_type = 'R'
and exists(
select 1
from all_constraints
where constraint_name=a.r_constraint_name
and constraint_type in ('P', 'U')
and table_name = :OBJECT_NAME
and owner = :OBJECT_OWNER)
order by table_name, constraint_name]]>
</sql>
</query>
</item>
</items>
答案 9 :(得分:0)
我喜欢使用直接的SQL查询来执行此操作,而不是搞砸SQL Developer应用程序。
这就是我刚做到的方式。最好通读此书并了解发生了什么,因此您可以对其进行调整以满足您的需求...
WITH all_primary_keys AS (
SELECT constraint_name AS pk_name,
table_name
FROM all_constraints
WHERE owner = USER
AND constraint_type = 'P'
)
SELECT ac.table_name || ' table has a foreign key called ' || upper(ac.constraint_name)
|| ' which references the primary key ' || upper(ac.r_constraint_name) || ' on table ' || apk.table_name AS foreign_keys
FROM all_constraints ac
LEFT JOIN all_primary_keys apk
ON ac.r_constraint_name = apk.pk_name
WHERE ac.owner = USER
AND ac.constraint_type = 'R'
AND ac.table_name = nvl(upper(:table_name), ac.table_name)
ORDER BY ac.table_name, ac.constraint_name
;