Tuesday, June 25, 2013

Oracle Foreign Key References

Query to find out which other tables reference a specified table in a foreign key constraint:
SELECT c_src.TABLE_NAME as SRC_TABLE,
c_list.CONSTRAINT_NAME as NAME,
substr(c_src.COLUMN_NAME, 1, 20) as SRC_COLUMN,
c_dest.TABLE_NAME as DEST_TABLE,
substr(c_dest.COLUMN_NAME, 1, 20) as DEST_COLUMN
FROM ALL_CONSTRAINTS c_list, ALL_CONS_COLUMNS c_src, ALL_CONS_COLUMNS c_dest
WHERE c_list.CONSTRAINT_NAME = c_src.CONSTRAINT_NAME
AND c_list.R_CONSTRAINT_NAME = c_dest.CONSTRAINT_NAME
AND c_list.CONSTRAINT_TYPE = 'R'
AND c_dest.TABLE_NAME = 'table name'
GROUP BY c_list.CONSTRAINT_NAME, c_src.TABLE_NAME, 
    c_src.COLUMN_NAME, c_dest.TABLE_NAME,    c_dest.COLUMN_NAME;

No comments:

Post a Comment