获取引用表名和表列

时间:2012-01-02 02:57:39

标签: sql oracle foreign-keys

我想创建如下结果的查询:

  

column_name,constraint_name,constraint_type,referenced_table_name,referenced_column_name

但是在构建referenced_table_name和referenced_column_name

时遇到了问题

这是我的查询

SELECT cols.column_name, cons.constraint_name, cons.constraint_type
FROM all_constraints cons,  all_cons_columns cols
WHERE cols.table_name = 'EMPLOYEES'
AND cons.owner = 'HR'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner

它只显示column_name,constraint_name和constraint_type。我怎么展示其余的?感谢任何帮助......

1 个答案:

答案 0 :(得分:2)

这样的东西?

select cols1.column_name
, r1.constraint_name
, r1.constraint_type 
, cols2.table_name
, cols2.column_name
from all_constraints r1
,    all_cons_columns cols1
,    all_cons_columns cols2
WHERE r1.constraint_name = cols1.constraint_name
AND   r1.owner = cols1.owner
and   r1.r_owner = cols2.owner(+)
and   r1.r_constraint_name = cols2.constraint_name(+)
AND cols1.table_name = 'EMPLOYEES' 
AND r1.owner = 'HR' 
/