Query a Table's Foreign Key relationships

后端 未结 9 1877
旧时难觅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:52

    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
    

提交回复
热议问题