Every so often I have to remove a user from our database. Users are stored in a table called TPM_USER. The problem is, there’s a bunch of tables that have a foreign key constraint on TPM_USER.USERID. To make matters worse, these constraints are using ON CASCADE DELETE. The other day, I deleted a user that I thought was not in use by anything (it was a duplicate user mistakenly created a few hours before), but it wiped out a bunch of important data without any warning.
Personally, I hate cascading deletes. I think they’re dangerous and should only be used if the two entities are indeed mutually dependent. I’d love to remove them all, but this schema is rather complicated and it would probably be too big of a change to bite off at this time.
My question: Before running a DELETE statement on TPM_USER, can I have Oracle tell me exactly what will be deleted as a result? Or, can I temporarily disable any cascading and get an error if any foreign keys are violated instead?
Thanks!
In answer to your question I don’t know of any easy way to show what would be deleted in a cascade delete statement. You could script it but it would be quite complex since you will have to walk the dependency tree dynamically only showing records pertinent to your delete criteria.
Anyway, you can disable the constraint by using:
( use ‘enable’ to re-enable it ).
Obviously you should be aware if you do this and another user deletes from the same table then Oracle won’t be enforcing that constraint and bad things could happen.
You can get a list of all table/constraint names referencing TPM_USER.USERID with:
( use all_constraints if you have cross-schema dependencies )