Developer and DBA often requires Parent table to Child table relationship for various tables in the Schema.
I found couple of them but not with my satisfaction.
So posting this for them which one can understand easily and for very beginner:
Connect to the schema user for which this relation is required and then run the below query.
SELECT B.TABLE_NAME AS PARENT_TABLE,
A.R_CONSTRAINT_NAME AS PARENT_TABLE_CONSTRAINT,
B.CONSTRAINT_TYPE AS PARENT_CONS_TYPE,
A.TABLE_NAME AS CHILD_TABLE,
A.CONSTRAINT_NAME AS CHILD_TABLE_CONSTRAINT_NAME,
A.CONSTRAINT_TYPE AS CHILD_CONSTRAINT_TYPE,
A.R_CONSTRAINT_NAME AS CHILD_TABLE_REF_TO_CONS from USER_CONSTRAINTS A
JOIN USER_CONSTRAINTS B on A.R_CONSTRAINT_NAME=B.CONSTRAINT_NAME AND B.CONSTRAINT_TYPE='P'
where A.R_CONSTRAINT_NAME IS NOT NULL AND
(A.TABLE_NAME in(select TABLE_NAME from USER_CONSTRAINTS where OWNER=&2)) AND (A.CONSTRAINT_TYPE='R') AND (A.TABLE_NAME=&1)
or
select A.TABLE_NAME as PARENT_TABLE,A.CONSTRAINT_TYPE as PARENT_CONS_TYPE,B.TABLE_NAME AS CHILD_TABLE_NAME,B.CONSTRAINT_TYPE as CHILD_TAB_CONS_TYPE,B.R_CONSTRAINT_NAME AS CHILD_TAB_CONS,
(select COLUMN_NAME from user_cons_columns uc where uc.CONSTRAINT_NAME= B.CONSTRAINT_NAME ) CHILD_TABLE_REF_COLUMN
from USER_CONSTRAINTS A join USER_CONSTRAINTS B on A.CONSTRAINT_NAME=B.R_CONSTRAINT_NAME where A.TABLE_NAME='&1' and B.CONSTRAINT_TYPE='R' AND A.CONSTRAINT_TYPE='P'
Just Pass on the schema name and table name within quotes and enjoy!!!!
I found couple of them but not with my satisfaction.
So posting this for them which one can understand easily and for very beginner:
Connect to the schema user for which this relation is required and then run the below query.
SELECT B.TABLE_NAME AS PARENT_TABLE,
A.R_CONSTRAINT_NAME AS PARENT_TABLE_CONSTRAINT,
B.CONSTRAINT_TYPE AS PARENT_CONS_TYPE,
A.TABLE_NAME AS CHILD_TABLE,
A.CONSTRAINT_NAME AS CHILD_TABLE_CONSTRAINT_NAME,
A.CONSTRAINT_TYPE AS CHILD_CONSTRAINT_TYPE,
A.R_CONSTRAINT_NAME AS CHILD_TABLE_REF_TO_CONS from USER_CONSTRAINTS A
JOIN USER_CONSTRAINTS B on A.R_CONSTRAINT_NAME=B.CONSTRAINT_NAME AND B.CONSTRAINT_TYPE='P'
where A.R_CONSTRAINT_NAME IS NOT NULL AND
(A.TABLE_NAME in(select TABLE_NAME from USER_CONSTRAINTS where OWNER=&2)) AND (A.CONSTRAINT_TYPE='R') AND (A.TABLE_NAME=&1)
or
select A.TABLE_NAME as PARENT_TABLE,A.CONSTRAINT_TYPE as PARENT_CONS_TYPE,B.TABLE_NAME AS CHILD_TABLE_NAME,B.CONSTRAINT_TYPE as CHILD_TAB_CONS_TYPE,B.R_CONSTRAINT_NAME AS CHILD_TAB_CONS,
(select COLUMN_NAME from user_cons_columns uc where uc.CONSTRAINT_NAME= B.CONSTRAINT_NAME ) CHILD_TABLE_REF_COLUMN
from USER_CONSTRAINTS A join USER_CONSTRAINTS B on A.CONSTRAINT_NAME=B.R_CONSTRAINT_NAME where A.TABLE_NAME='&1' and B.CONSTRAINT_TYPE='R' AND A.CONSTRAINT_TYPE='P'
Just Pass on the schema name and table name within quotes and enjoy!!!!
No comments:
Post a Comment