Solved

Oracle Constraint Selection Query

Posted on 2014-01-20
3
488 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 11

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

734 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