本文介绍了外键列表及其引用的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试查找一个查询,该查询将为我返回表的外键以及它们引用的表和列的列表.
I'm trying to find a query which will return me a list of the foreign keys for a table and the tables and columns they reference. I am half way there with
SELECT a.table_name,
a.column_name,
a.constraint_name,
c.owner
FROM ALL_CONS_COLUMNS A, ALL_CONSTRAINTS C
where A.CONSTRAINT_NAME = C.CONSTRAINT_NAME
and a.table_name=:TableName
and C.CONSTRAINT_TYPE = 'R'
但是我仍然需要知道该键引用了哪个表和主键.我怎么得到的?
But I still need to know which table and primary key are referenced by this key. How would I get that?
推荐答案
在表ALL_CONSTRAINTS
的列r_owner
和r_constraint_name
中描述了所引用的主键.这将为您提供所需的信息:
The referenced primary key is described in the columns r_owner
and r_constraint_name
of the table ALL_CONSTRAINTS
. This will give you the info you want:
SELECT a.table_name, a.column_name, a.constraint_name, c.owner,
-- referenced pk
c.r_owner, c_pk.table_name r_table_name, c_pk.constraint_name r_pk
FROM all_cons_columns a
JOIN all_constraints c ON a.owner = c.owner
AND a.constraint_name = c.constraint_name
JOIN all_constraints c_pk ON c.r_owner = c_pk.owner
AND c.r_constraint_name = c_pk.constraint_name
WHERE c.constraint_type = 'R'
AND a.table_name = :TableName
这篇关于外键列表及其引用的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!