473,394 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Help Required in Query

Hi All,

We have a requirement to find out the relationships among tables in a particular schema and to arrange the table names from parent table to child table. That is the table (say A) which is referring to no other table should come first and the table (say B) which refers table A should come after A.

Please let me know how this ordered list of tables can be obtained from the oracle data dictionary. We have to do this using a SQl query.

Thanks and Regards,
Suman
Nov 16 '06 #1
2 3693
pragatiswain
96 Expert
Hi Suman,
I don't have a Oracle DB right now. So, I have not tested the following. Still I have commented all the queries for better understanding and hope this will help you.

USER_CONSTRAINTS view contains CONSTRAINT_TYPE (R -> Foreign key, P->Primary Key)
If CONSTRAINT_TYPE = 'R',R_CONSTRAINT_NAME contains the corresponding Primary Key Name

-- GIVES THE RESULT 'TABLES ONLY WITH PRIMARY KEYS'
SELECT TABLE_NAME, 'P' KEY_FLAG FROM
(SELECT TABLE_NAME,
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_NAME IN (
SELECT R_CONSTRAINT_NAME PK_KEY
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'R')
MINUS
SELECT TABLE_NAME
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'R')
UNION ALL
-- GIVES THE RESULT 'TABLES WITH PRIMARY KEYS AND FOREIGN KEYS'
SELECT TABLE_NAME, 'B' KEY_FLAG FROM
(SELECT TABLE_NAME
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_NAME IN (
SELECT R_CONSTRAINT_NAME PK_KEY
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'R')
INTERSECT
SELECT DISTINCT TABLE_NAME
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'R')
UNION ALL
-- GIVES THE RESULT 'OTHER TABLES'
-- SELECT ALL TABLES FROM USER_TABLES MINUS ABOVE CASES
SELECT TABLE_NAME, 'O' KEY_FLAG FROM
(
SELECT TABLE_NAME FROM USER_TABLES
MINUS
(
SELECT TABLE_NAME FROM
(SELECT TABLE_NAME,
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_NAME IN (
SELECT R_CONSTRAINT_NAME PK_KEY
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'R')
MINUS
SELECT TABLE_NAME
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'R')
UNION ALL
SELECT TABLE_NAME FROM
(SELECT TABLE_NAME
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_NAME IN (
SELECT R_CONSTRAINT_NAME PK_KEY
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'R')
INTERSECTION
SELECT DISTINCT TABLE_NAME
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'R')
)
)

Anyone having better ideas, please post.
Nov 16 '06 #2
suvam
31
u may try with a Query using the Connect by---Prior with clause .
Dec 6 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: iainw | last post by:
HI All, 1st post here, i wonder if you can help. We are about to upload CMS t a windows server and keep getting 2 errors below. We need to go LIVE an it's delaying us. An error occured when...
2
by: Tarren | last post by:
Hi: The problem I am having is when I validate an xml file to a schema, it is erroring out every element. I think this has something to do with me defining/referencing the namespaces. I have...
2
by: Jeff Blee | last post by:
I am hoping someone can help me. I am making a Access 97 app for a person and have run up against a problem to do with MS Graph. There is a table that has a number of data elements and a date field...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
2
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to...
3
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
0
by: bob.herbst | last post by:
I am trying to write a form that will add a guest to a guest table in a MySQL database and then reload the page to display any guests that have been added as well as redisplay the guest form to add...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
8
by: Sham | last post by:
I am trying to perform the following query on a table that has been indexed using Full Text Search. The table contains multiple columns than have been indexed. (Below, all xml columns are...
1
by: yfangl09 | last post by:
I have one query with a list of people and required courses they have to take and another with the same people and courses they have already taken. How do I generate a query with required courses...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.