tags:

views:

42

answers:

2

Is there a query I can do to find which tables have foreign keys on a given table? Our DBA does not believe in (or understand?) "ON DELETE CASCADE", so I when I delete something from a table, I want to make sure I delete all the dependent stuff first.

(Note, I don't need to find the tables programmatically, I can do that in SQL*Plus.)

+1  A: 

Check all_constraints and all_cons_columns dictionaries.

be here now
I don't see a column in those dictionaries that tell you which table the foreign key is for, so if I `select * from all_constraints where table_name = 'CLIENT'` and it shows me all the indexes and constraints on `CLIENT`, but I can't find a query that shows me which tables have a foreign key on CLIENT.
Paul Tomblin
Oh, but if I `select * from all_constraints where r_constraint_name = 'PK__CLIENT'` it does what I want. Mostly.
Paul Tomblin
+2  A: 
SELECT dc.constraint_name, dc.constraint_type, dc.owner, dc.table_name
FROM dba_cons_columns dcc 
JOIN dba_constraints dc ON (dcc.constraint_name = dc.r_constraint_name and dc.owner = dcc.owner)
WHERE dcc.owner = 'OWNER_NAME' and dcc.table_name = 'TABLE_NAME';
RC
I had to change the second line to `FROM dba_cons_columns dcc JOIN dba_constraints dc ON (dcc.constraint_name = dc.r_constraint_name and dc.owner = dcc.owner)` because multiple owners have the same constraint names.
Paul Tomblin
Good catch. I've updated my answer. Thanks.
RC