Home » SQL & PL/SQL » SQL & PL/SQL » Query to find tables/views with cross referencing foreign keys (Oracle 8.1.7.4.1, Windows server 2003 R2)
Query to find tables/views with cross referencing foreign keys [message #603224] Thu, 12 December 2013 05:48 Go to next message
oraclebabe
Messages: 7
Registered: December 2013
Junior Member
Hello,
How can I query the tables or views in a particular schema to find those tables or views with foreign keys that cross reference each other?
Thanks in advance.
Re: Query to find tables/views with cross referencing foreign keys [message #603225 is a reply to message #603224] Thu, 12 December 2013 06:12 Go to previous messageGo to next message
martijn
Messages: 286
Registered: December 2006
Location: Netherlands
Senior Member
Play around a little with user_constraints and user_cons_columns.

See if you can work it out Smile
Re: Query to find tables/views with cross referencing foreign keys [message #603227 is a reply to message #603224] Thu, 12 December 2013 06:18 Go to previous messageGo to next message
tigsav
Messages: 49
Registered: April 2012
Member
Select a.CONSTRAINT_NAME,b.CONSTRAINT_TYPE,a.TABLE_NAME,a.COLUMN_NAME,a.POSITION,b.STATUS 
from USER_CONS_COLUMNS a ,USER_CONSTRAINTS b
where a.CONSTRAINT_NAME = b.CONSTRAINT_NAME
order by TABLE_NAME,CONSTRAINT_NAME,CONSTRAINT_TYPE,COLUMN_NAME,POSITION,STATUS
/
Re: Query to find tables/views with cross referencing foreign keys [message #603232 is a reply to message #603224] Thu, 12 December 2013 07:46 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Just for fun:
SQL> create table t1 (id1 int primary key, id2 int);

Table created.

SQL> create table t2 (id2  int primary key, id1 int references t1);

Table created.

SQL> alter table t1 add foreign key (id2) references t2;

Table altered.

SQL> select a.table_name, b.table_name
  2  from user_constraints a, user_constraints b, user_constraints c, user_constraints d
  3  where a.owner = user and a.constraint_type = 'R'
  4    and b.owner = a.r_owner and b.constraint_name = a.r_constraint_name
  5    and c.owner = b.owner and c.table_name = b.table_name and c.constraint_type = 'R'
  6    and d.owner = c.r_owner and d.constraint_name = c.r_constraint_name
  7    and d.owner = a.owner and d.table_name = a.table_name
  8  order by 1, 2
  9  /
TABLE_NAME                     TABLE_NAME
------------------------------ ------------------------------
EMP                            EMP
T1                             T2
T2                             T1

Re: Query to find tables/views with cross referencing foreign keys [message #603241 is a reply to message #603232] Thu, 12 December 2013 09:01 Go to previous message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Or with hierarchical query:

with t as (
           select  a.owner,
                   a.constraint_name,
                   a.table_name,
                   a.r_owner,
                   a.r_constraint_name,
                   b.table_name r_table_name
             from  dba_constraints a,
                   dba_constraints b
             where b.owner = a.r_owner
               and b.constraint_name = a.r_constraint_name
          )
select  connect_by_root owner parent_table_owner,
        connect_by_root table_name parent_table,
        owner child_table_owner,
        table_name child_table
  from  t
  where level > 1
    and (
            connect_by_iscycle = 1
         or
            connect_by_root(owner || '.' || table_name) = owner || '.' || table_name
        )
  start with owner = 'SCOTT'
  connect by nocycle owner = prior r_owner
                 and table_name = prior r_table_name
/

PARENT_TABLE_OWNER PARENT_TABLE CHILD_TABLE_OWNER CHILD_TABLE
------------------ ------------ ----------------- ---------------
SCOTT              CHICKEN      SCOTT             EGG
SCOTT              EGG          SCOTT             CHICKEN
SCOTT              EMP          SCOTT             EMP
SCOTT              T1           SCOTT             T2
SCOTT              T2           SCOTT             T1

SCOTT@orcl >  


SY.
Previous Topic: Case Statement
Next Topic: Global variable in Oracle Object (Super/Sub) Types?
Goto Forum:
  


Current Time: Fri Apr 26 22:23:46 CDT 2024