Query a Table's Foreign Key relationships

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

    All constraints for one table

    select 
    
        uc.OWNER,
        uc.constraint_name as TableConstraint1,
        uc.r_constraint_name as TableConstraint2,
        uc.constraint_type as constrainttype1,
        us.constraint_type as constrainttype2,
        uc.table_name as Table1,us.table_name as Table2,
        ucc.column_name as TableColumn1, 
        uccs.column_name as TableColumn2
    from user_constraints uc
        left outer join user_constraints us on uc.r_constraint_name = us.constraint_name
        left outer join USER_CONS_COLUMNS ucc on ucc.constraint_name = uc.constraint_name
        left outer join USER_CONS_COLUMNS uccs on uccs.constraint_name = us.constraint_name
    where uc.OWNER ='xxxx' and uc.table_name='xxxx' 
    

提交回复
热议问题