Solved

Oracle Constraint Selection Query

Posted on 2014-01-20
3
474 Views
Last Modified: 2014-01-20
Hello. So I'm trying to correct someone else's work on something in Oracle. This query is supposed to return a list of foreign key constraints from the database. However, it seems like this query is only partially joined up. I'm having trouble figuring out how to tie it all together. Can anyone see what is missing? (these are all system tables).

  SELECT y.table_name,
    y.column_name
  FROM user_cons_columns x,
    user_cons_columns y,  
    user_constraints z
  WHERE x.constraint_name = z.constraint_name
  AND y.constraint_name   = z.r_constraint_name
  AND z.constraint_type   ='R'
  AND x.table_name        ='tablename
  AND x.column_name       ='colname'
 

The idea is to return foreign key (y) table and column name for the where clause's table (x) and column (x).
0
Comment
Question by:Jay Redd
3 Comments
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 250 total points
ID: 39795935
So, X is a child and given FK column in X, you want to find the corresponding PK/UK column(s) of Parent Y?

If so,  then what you have should work as long as all of the constraints are within your own schema.  IF not then try using the ALL_xxxx or DBA_xxxx views instead of the USER_xxxx views.

If I'm not understanding then please provide some tables and constraints to illustrate what you're trying to get
0
 
LVL 10

Accepted Solution

by:
HuaMinChen earned 250 total points
ID: 39795938
Try this instead
SELECT a.table_name, a.column_name, a.constraint_name, c.owner, 
       -- referenced pk
       c.r_owner, c_pk.table_name r_table_name, c_pk.constraint_name r_pk
  FROM all_cons_columns a
  JOIN all_constraints c ON a.owner = c.owner
                        AND a.constraint_name = c.constraint_name
  JOIN all_constraints c_pk ON c.r_owner = c_pk.owner
                           AND c.r_constraint_name = c_pk.constraint_name
 WHERE c.constraint_type = 'R'
   AND a.table_name = 'your_tab';

Open in new window

0
 

Author Comment

by:Jay Redd
ID: 39795963
Thanks to both of you! It looks like my query did work. It matches yours HuaMinChen. I assumed it was a problem with my query but the person who was writing this did not take into account that it would return more than one row ever...
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question