在StackOverflow中,我可以找到关于how to find all foreign key constraints in SQL Server的帖子。
我只能在Oralce(List of foreign keys and the tables they reference)中找到有关如何查找表的所有外键约束的帖子。
我的问题是:如何列出所有表的所有外键约束,而不是Oracle中的表。我的愿望答案将是this one,但对于Oracle。
P.S。我正在使用oralce 11g快递版。我的oracle GUI管理工具是Orace SQL开发人员。
答案 0 :(得分:1)
我会那样做;
如果您希望它与同一行中的所有列聚合
select distinct c1.owner, c1.table_name, c1.constraint_name, c2.owner, c2.table_name,
listagg(c2.column_name,',') WITHIN GROUP ( ORDER BY C2.POSITION) OVER ( PARTITION BY c1.owner, c1.table_name, c1.constraint_name, c2.owner, c2.table_name ) column_list
from dba_constraints c1
JOIN dba_cons_columns c2
ON c1.R_CONSTRAINT_NAME=C2.CONSTRAINT_NAME and c1.r_owner=c2.owner
where C1.constraint_type = 'R'
或类似的,如果你想要每列一行
select c1.owner, c1.table_name, c1.constraint_name, c2.owner, c2.table_name, c2.column_name
from dba_constraints c1
JOIN dba_cons_columns c2
ON c1.R_CONSTRAINT_NAME=C2.CONSTRAINT_NAME and c1.r_owner=c2.owner
where C1.constraint_type = 'R'
order by c1.owner, c1.table_name, c1.constraint_name, c2.position
或者如果您想要引用列详细信息和引用的列详细信息
with constraint_colum_list as ( select owner, table_name, constraint_name, listagg(column_name,',') WITHIN GROUP ( order by position ) as column_list
FROM DBA_CONS_COLUMNS GROUP BY owner, table_name, constraint_name )
select distinct c1.owner, c1.table_name, c1.constraint_name, c2.column_list, c3.owner, c3.table_name, c3.constraint_name, c3.column_list
from DBA_constraints c1
JOIN constraint_colum_list c2 ON c1.CONSTRAINT_NAME=C2.CONSTRAINT_NAME and c1.owner=c2.owner
JOIN constraint_colum_list c3 ON C1.R_CONSTRAINT_NAME=C3.CONSTRAINT_NAME AND C1.R_OWNER=C3.owner
where C1.constraint_type = 'R'
-- AND c1.owner = 'YOUR_SCHEMA';
答案 1 :(得分:0)
不要在where where条件下使用Table name,你将获得所有表的FK约束。
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 C.CONSTRAINT_TYPE = 'R'
AND C.OWNER = 'MyDBLoginName';
答案 2 :(得分:0)
这个答案是基于LauDec的解决方案。
由于我只需要我/用户指定的所有外键约束,所以我将使用以下SQL:
with constraint_colum_list as ( select owner, table_name, constraint_name, listagg(column_name,',') WITHIN GROUP ( order by position ) as column_list
FROM USER_CONS_COLUMNS GROUP BY owner, table_name, constraint_name )
select distinct c1.owner, c1.table_name, c1.constraint_name, c2.column_list, c3.owner, c3.table_name, c3.constraint_name, c3.column_list
from USER_CONSTRAINTS c1
JOIN constraint_colum_list c2 ON c1.CONSTRAINT_NAME=C2.CONSTRAINT_NAME and c1.owner=c2.owner
JOIN constraint_colum_list c3 ON C1.R_CONSTRAINT_NAME=C3.CONSTRAINT_NAME AND C1.R_OWNER=C3.owner
where C1.constraint_type = 'R';
答案 3 :(得分:0)
下面的查询给出了父表和子表的表名,列名以及每个表的约束名称。
select a.table_name child_table, a.column_name child_column, b.table_name parent_table, b.column_name parent_column, a.position, a.constraint_name child_constraint, b.constraint_name parent_constraint
from all_cons_columns a
join all_constraints lc on a.owner = lc.owner and a.constraint_name = lc.constraint_name
join all_constraints rc on lc.r_owner = rc.owner and lc.r_constraint_name = rc.constraint_name
join all_cons_columns b on rc.owner = b.owner and rc.constraint_name = b.constraint_name and a.position = b.position
where a.owner = '<Schema Name>'
and lc.constraint_type = 'R'
order by a.constraint_name, a.table_name, a.position
以下是上述SQL中使用的表的简要说明: