Solved

Oracle Constraint Selection Query

Posted on 2014-01-20
3
466 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.

Join & Write a Comment

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  â€¦
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
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now