对于给定的表'foo',我需要一个查询来生成一组具有指向foo的外键的表。我正在使用Oracle 10G。
答案 0 :(得分:41)
这应该有效(或接近):
select table_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 here>');
答案 1 :(得分:4)
以下陈述应该给予孩子及其所有后代。我在Oracle 10数据库上测试过它。
SELECT level, main.table_name parent,
link.table_name child
FROM user_constraints main, user_constraints link
WHERE main.constraint_type IN ('P', 'U')
AND link.r_constraint_name = main.constraint_name
START WITH main.table_name LIKE UPPER('&&table_name')
CONNECT BY main.table_name = PRIOR link.table_name
ORDER BY level, main.table_name, link.table_name
答案 2 :(得分:2)
以下是如何将Mike的查询更进一步从约束名称中获取列名:
select * from user_cons_columns
where constraint_name in (
select 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 name here>'));
答案 3 :(得分:1)
链接到Oracle Database Online Documentation
您可能想要探索Data Dictionary views。他们有前缀:
样品:
select * from dictionary where table_name like 'ALL%'
继续Mike的示例,您可能希望生成脚本以启用/禁用约束。我只修改了第一行中的'select'。
select 'alter table ' || TABLE_NAME || ' disable constraint ' || 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 here>');
答案 4 :(得分:1)
我知道回答有点迟,但我还是要回答,上面的一些答案非常复杂,因此这是一个更简单的方法。
`SELECT a.table_name child_table, a.column_name child_column, a.constraint_name, b.table_name parent_table, b.column_name parent_column FROM all_cons_columns a JOIN all_constraints c ON a.owner = c.owner AND a.constraint_name = c.constraint_name join all_cons_columns b on c.owner = b.owner and c.r_constraint_name = b.constraint_name WHERE c.constraint_type = 'R' AND a.table_name = 'your table name'`
答案 5 :(得分:0)
下载适用于10G的Oracle参考指南,其中介绍了数据字典表。
上面的答案很好,但请查看可能与约束相关的其他表格。
SELECT * FROM DICT WHERE TABLE_NAME LIKE '%CONS%';
最后,获取像Toad或SQL Developer这样的工具,它允许你在UI中浏览这些东西,你需要学习使用这些表,但你也应该使用UI。
答案 6 :(得分:0)
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 :(得分:0)
select acc.table_name, acc.constraint_name
from all_cons_columns acc
inner join all_constraints ac
on acc.constraint_name = ac.constraint_name
where ac.r_constraint_name in (
select constraint_name
from all_constraints
where table_name='yourTable'
);
答案 8 :(得分:0)
一张桌子的所有约束
select
uc.OWNER,
uc.constraint_name as TableConstraint1,
uc.r_constraint_name as TableConstraint2,
uc.constraint_type as constrainttype1,
us.constraint_type as constrainttype2,
uc.table_name as Table1,us.table_name as Table2,
ucc.column_name as TableColumn1,
uccs.column_name as TableColumn2
from user_constraints uc
left outer join user_constraints us on uc.r_constraint_name = us.constraint_name
left outer join USER_CONS_COLUMNS ucc on ucc.constraint_name = uc.constraint_name
left outer join USER_CONS_COLUMNS uccs on uccs.constraint_name = us.constraint_name
where uc.OWNER ='xxxx' and uc.table_name='xxxx'
答案 9 :(得分:0)
在这里加上我的两分钱。
此查询将返回所有带有子列和父列的外键,即使在多列上存在外键时也能完美匹配:
SELECT a.table_name child_table, a.column_name child_column, a.constraint_name,
b.table_name parent_table, b.column_name parent_column
FROM all_cons_columns a
JOIN all_constraints c ON a.owner = c.owner AND a.constraint_name = c.constraint_name
JOIN all_cons_columns b ON c.owner = b.owner AND c.r_constraint_name = b.constraint_name AND b.position = a.position
WHERE c.constraint_type = 'R'
(灵感来自@arvinq aswer)