Query a Table's Foreign Key relationships

后端 未结 9 1876
旧时难觅i
旧时难觅i 2020-12-03 01:40

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.

9条回答
  •  我在风中等你
    2020-12-03 01:54

    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'`
    

提交回复
热议问题