Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SQL Exercise for FK constraints

578585Jun 20 2007 — edited Jun 20 2007
Hello,

I am trying to write a SQL query to have a resultset with the following 6 columns:

referent table, referent constraint name, referent column name, referred table, referred constraint name, referred column name.

I would like to have a row for each FK present in the DB.

EG: if in one table SELLER there is the column SHOP_ID that is FK with constraint name SELLER_FK that references constraint SHOP_PK in table SHOP for column ID, I would like to have a row like this:

TABLE CONS COL R_TABLE R_CONS R_COL
-------------------------------------------------------------------------------------------------
SELLER SHOP_FK SHOP_ID SHOP SHOP_PK ID

I would like a query that returns a line as above for each FK in the DB. I am using the views "all_cons_columns" and "user_constraints" I tried this query but it does not work (because the subquery returns more than a row):

SELECT A.table_name, A.constraint_name, (SELECT column_name FROM all_cons_columns B, user_constraints A WHERE ( B.constraint_name=A.constraint_name AND A.constraint_type='R' )) AS colonna, B.table_name, B.constraint_name, B.column_name FROM user_constraints A, all_cons_columns B WHERE A.r_constraint_name=B.constraint_name;

Is it possible to create such a query?

Thanks,

Enrico
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 18 2007
Added on Jun 20 2007
2 comments
458 views