Solved

Oracle Constraint Selection Query

Posted on 2014-01-20
3
480 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 74

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PL/SQL Two changes 7 41
Help on model clause 5 47
return value in based on value passed 6 37
Oracle Errors 11 43
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

679 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