Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 593849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:53:22+00:00 2026-05-13T15:53:22+00:00

I had an idea that I could write a query to find all the

  • 0

I had an idea that I could write a query to find all the descendent 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:53:23+00:00Added an answer on May 13, 2026 at 3:53 pm

    You want something like this:

    select t.table_name, level,lpad(' ', 2 * (level - 1))||t.table_name 
    from user_tables t
    join user_constraints c1 
        on (t.table_name = c1.table_name 
        and c1.constraint_type in ('U', 'P'))
    left join user_constraints c2 
        on (t.table_name = c2.table_name 
        and c2.constraint_type='R')
    start with t.table_name = 'ROOT_TAB'
    connect by prior c1.constraint_name = c2.r_constraint_name
    

    The problem with the original query is that uc.constraint_name for the child table is the name of the foreign key. That is fine for connecting the first child to the root table, but it is not what you need to connect the children on the second level to the first. That is why you need to join against the constraints twice — once to get the table’s primary key, once to get the foreign keys.

    As an aside, if you are going to be querying the all_* views rather than the user_* views, you generally want to join them on table_name AND owner, not just table_name. If multiple schemas have tables with the same name, joining on just table_name will give incorrect results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had the idea of a search engine that would index web items like
I am trying to write a MySQL insert that only inserts based on the
I had an idea, if I add a python .py file to my C#
I had an idea for a client-side language other than JavaScript, and I'd like
I had an idea I was mulling over with some colleagues. None of us
I had this idea of creating a count down timer, like 01:02, on the
It seems like Microsoft had a great idea with the ObservableCollection. They are great
I was wondering if anyone had a better idea how to do this. atm
I'm trying to implement a data compression idea I've had, and since I'm imagining
Had an interesting discussion with some colleagues about the best scheduling strategies for realtime

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.