0

我想确定几个表之间的外键关系。给定一个表名和列名,我如何识别其他哪些表与该列有外键关系?

(table name, column name) -> (list of tables,columns with FK dependency)
4

2 回答 2

0

我使用此查询来比较外键引用的列,因为我们的 DBA 要求它们具有相同的类型和相同的精度。也许它可以帮助你。

您可以根据您的权限使用前缀 DBA、ALL 或 USER:

SELECT   uc.constraint_name,ucc1.TABLE_NAME,ucc1.column_name,a.data_type||'('||nvl(a.data_precision,a.data_length)||')' length,
         ucc2.TABLE_NAME references_table,ucc2.column_name references_column, b.data_type||'('||nvl(b.data_precision,b.data_length)||')' length
FROM     user_constraints uc
,        user_cons_columns ucc1
,        user_cons_columns ucc2
,        user_tab_columns a
,        user_tab_columns b
WHERE    uc.constraint_name = ucc1.constraint_name
and      a.column_name=ucc1.column_name
and      b.column_name=ucc1.column_name
and      uc.table_name=a.table_name
and      uc.table_name=b.table_name
AND      uc.r_constraint_name = ucc2.constraint_name
AND      ucc1.POSITION = ucc2.POSITION 
AND      uc.constraint_type = 'R'
ORDER BY ucc1.TABLE_NAME
,        uc.constraint_name;
于 2013-07-18T22:33:30.083 回答
0

你可能会做这样的事情:

select x.owner || '.' || x.table_name || '.' || x.column_name childcol,
       y.owner || '.' || y.table_name || '.' || y.column_name parentcol,
       z.constraint_name
  from all_cons_columns x,
       all_cons_columns y,
       all_constraints z
 where x.constraint_name = z.constraint_name
   and z.owner = x.owner
   and y.constraint_name = z.r_constraint_name
   and z.constraint_type = 'R'

您还需要在谓词中包含您要查找的表是外键关系中的父表还是子表。

于 2013-07-18T21:47:15.203 回答