Wednesday, March 22, 2017

Find out the columns used in a referencial integrity constraints

Using the data dictionary views USER_CONSTRAINTS and USER_CONS_COLUMNS, it is possible to find out what columns are used in a referencial integrity constraint:


SELECT 
  UCC1.TABLE_NAME, 
  UCC1.COLUMN_NAME, 
  UCC1.CONSTRAINT_NAME, 
  UC.R_CONSTRAINT_NAME, 
  UCC2.TABLE_NAME
FROM USER_CONSTRAINTS UC , 
  USER_CONS_COLUMNS UCC1, 
  USER_CONS_COLUMNS UCC2
WHERE UC.CONSTRAINT_TYPE = 'R'
  AND UCC1.CONSTRAINT_NAME = UC.CONSTRAINT_NAME
  AND UCC2.CONSTRAINT_NAME = UC.R_CONSTRAINT_NAME