Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does CASCADE Delete execute as transaction?

Tags:

sql

cascade

I want to perform cascade delete for some tables in my database, but I'm interested in what happens in case there's a failure when deleting something. Will everything rollback?

like image 355
kjv Avatar asked Sep 22 '08 10:09

kjv


People also ask

What does cascade on delete do?

ON DELETE CASCADE constraint is used in MySQL to delete the rows from the child table automatically, when the rows from the parent table are deleted.

What is the effect when you enable the cascade delete related records?

When you enforce referential integrity and select the Cascade Delete Related Records check box, Access automatically deletes all records that reference the primary key when you delete the record that contains the primary key.

What does cascade delete mean in SQL?

Cascade delete- a relational database term used to describe the process by which child records are automatically deleted when their parent record is deleted- is, indeed, powerful. When used intentionally and correctly, cascade delete allows you to reduce the quantity of SQL statements needed to perform delete actions.

Is on delete cascade bad practice?

Yes, the use of ON DELETE CASCADE is fine, but only when the dependent rows are really a logical extension of the row being deleted. For example, it's OK for DELETE ORDERS to delete the associated ORDER_LINES because clearly, you want to delete this order, which consists of a header and some lines.


1 Answers

In general¹, yes, cascade deletes are done in the same transaction (or subtransaction) as your original delete. You should read the documentation of your SQL server, though.

¹ The exception is if you're using a database that doesn't support transactions, like MySQL with MyISAM tables.

like image 127
tzot Avatar answered Oct 03 '22 00:10

tzot