Query a Table's Foreign Key relationships

2019-01-11 03:28发布

For a given table 'foo', I need a query to generate a set of tables that have foreign keys that point to foo. I'm using Oracle 10G.

7条回答
在下西门庆
2楼-- · 2019-01-11 03:51

This should work (or something close):

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>'); 
查看更多
戒情不戒烟
3楼-- · 2019-01-11 03:58

The following statement should give the children and all of their descendents. I have tested it on an Oracle 10 database.

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
查看更多
我想做一个坏孩纸
4楼-- · 2019-01-11 03:59

I know it's kinda late to answer but let me answer anyway, some of the answers above are quite complicated hence here is a much simpler take.

       `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楼-- · 2019-01-11 04:02

Download the Oracle Reference Guide for 10G which explains the data dictionary tables.

The answers above are good but check out the other tables which may relate to constraints.

SELECT * FROM DICT WHERE TABLE_NAME LIKE '%CONS%';

Finally, get a tool like Toad or SQL Developer which allows you to browse this stuff in a UI, you need to learn to use the tables but you should use a UI also.

查看更多
闹够了就滚
6楼-- · 2019-01-11 04:03

Here's how to take Mike's query one step further to get the column names from the constraint names:

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>'));
查看更多
ゆ 、 Hurt°
7楼-- · 2019-01-11 04:06

link to Oracle Database Online Documentation

You may want to explore the Data Dictionary views. They have the prefixes:

  • User
  • All
  • DBA

sample:

select * from dictionary where table_name like 'ALL%' 

Continuing Mike's example, you may want to generate scripts to enable/disable the constraints. I only modified the 'select' in the first row.

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>');
查看更多
登录 后发表回答