I had an idea that I could write a query to find all the decendent tables of a root table, based on foreign keys.
Query looks like this:
select level, lpad(' ', 2 * (level - 1)) || uc.table_name as "TABLE", uc.constraint_name, uc.r_constraint_name
from all_constraints uc
where uc.constraint_type in ('R', 'P')
start with uc.table_name = 'ROOT_TAB'
connect by nocycle prior uc.constraint_name = uc.r_constraint_name
order by level asc;
The results I get look like this:
1 ROOT_TAB XPKROOTTAB 1 ROOT_TAB R_20 XPKPART_TAB 2 CHILD_TAB_1 R_40 XPKROOTTAB 2 CHILD_TAB_2 R_115 XPKROOTTAB 2 CHILD_TAB_3 R_50 XPKROOTTAB
This result is all the child tables of ROOT_TAB
, but the query does not recurse to the children of CHILD_TAB_1
, CHILD_TAB_2
, or CHILD_TAB_3
.
Recursive queries are new to me so I'm guessing I'm missing something in the connect by
clause, but I'm drawing a blank here. Is it actually possible to get the full hierarchy of ROOT_TAB
in a single query, or am I better off wrapping the query in a recursive procedure?
(Oracle 10g)