Monday 29 July 2013

Query to find parent and child tables and columns in Oracle






select b.owner , b.table_name , b.column_name CHILDCOL,
         C.OWNER , C.TABLE_NAME , C.COLUMN_NAME PARENTCOL,
          b.position,
         a.constraint_name,
         a.delete_rule,
         b.table_name bt,
         b.owner bo
    from all_cons_columns b,
         all_cons_columns c,
         all_constraints a
   where b.constraint_name = a.constraint_name
     and a.owner           = b.owner
     and b.position        = c.position
     and c.constraint_name = a.r_constraint_name
     and c.owner           = a.r_owner
     AND A.CONSTRAINT_TYPE = 'R'
     AND C.OWNER      =UPPER('&OWNER_NAME')
     AND C.TABLE_NAME =UPPER('&PARENT_TABLE_NAME')
     --and b.table_name like case when upper('&CHILD_TABLE_NAME') is null then '%'
       --                         else upper('&CHILD_TABLE_NAME') end
order by 7,6,4,2