I want to delete specific rows from 8 tables.
My problem is that the rows are connected with foreign key.
How can I delete all the data that connected to the specific rows that I want to delete?
My tables include definition tables (like id, name ,max value, min value…),
data tables (like id, user_id, definition_id,….) and history tables (save every change in data table).
I thought to use delete on cascade command but I could not find a way to use it.
DELETE CASCADEis an attribute of the foreign key constraint. Unfortunately it’s not something you can use as an option with aDELETEstatement (which would be really cool actually)If your foreign keys have not been declared as cascading you need to “work your way up”.
Unfortunately you did not show us your real table structure so let’s assume something like this:
main_table (main_id) child_one (id, main_id) child_two (id, id_one) child_three (id, id_two)(I know you said 8 tables, but for the sake of the demonstration I shortened it a bit, but that doesn’t change the underlying “strategy”)
Assuming you want to delete the row with
main_id = 42from `main_table:You first need to delete the rows from child_three using something like this:
Then delete the rows from child_two:
Then child_one:
And finally the main table:
Some SQL clients can actually generate those statements for you. I don’t know if SQL Developer can though.