4

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

2
  • 1
    Sounds more "does not believe". ON DELETE CASCADE does the same as looking into constraints and delete reference records. Commented Sep 16, 2010 at 14:28
  • However, ON DELETE CASCADE is notoriously slow; if it's a large job, it's usually better to delete all of the children first. Commented Sep 16, 2010 at 15:59

2 Answers 2

2
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'; 
Sign up to request clarification or add additional context in comments.

1 Comment

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

Check all_constraints and all_cons_columns dictionaries.

2 Comments

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.
Oh, but if I select * from all_constraints where r_constraint_name = 'PK__CLIENT' it does what I want. Mostly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.