Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Oracle Constraint Selection Query

Posted on 2014-01-20
3
Medium Priority
?
500 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 1000 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 1000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
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
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

670 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