Instead of deleting the child row and then writing another sql statement to delete the parent row I wanted to use one statement which will do both. FYI: we use Oracle database.
Update: I dont have a privilege to do DELETE ON CASCADE
Instead of deleting the child row and then writing another sql statement to delete the parent row I wanted to use one statement which will do both. FYI: we use Oracle database.
Update: I dont have a privilege to do DELETE ON CASCADE
WARNING! Will only delete where both parent AND child rows exist. Will NOT delete parents without children
You can only do it badly - i.e., using triggers.
Another (boring way, we have this in a database which, for unknown reason, don't use foreign keys as constraints - yes yes) to do this would be to create a trigger after (or before) delete.
You'll have to write another delete query, but just in the trigger.
But if you can't put delete cascade, I'm not sure you can add triggers...
If you always want to delete the children when you delete a parent row, you can declare the foreign key constraint so that Oracle does the child delete automatically
for example, will declare a parent table and a child table and automatically delete the child rows when you delete the parent row. If you don't want that sort of thing to be enforced automatically or you don't like the complexity that is added when things happen "automagically" in the background, you're probably stuck with using multiple
DELETE
statements.Define your foreign keys with cascading deletes. Then you only need to delete the "parent" row.