|
|
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 |
|
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 |
|
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 |
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.
|
|
|