SQL Exercise for FK constraints
578585Jun 20 2007 — edited Jun 20 2007Hello,
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