Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL on delete cascade. Test Example

Tags:

I am wondering about this test question. I prepared the example myself and tested it but I still feel unsure of the answer.

With the following:

CREATE TABLE foo (   id INT PRIMARY KEY AUTO_INCREMENT,    name INT )  CREATE TABLE foo2 (   id INT PRIMARY KEY AUTO_INCREMENT,    foo_id INT REFERENCES foo(id) ON DELETE CASCADE ) 

As far as I can see the answer is:

a. Two tables are created

Although there are also:

b. If a row in table foo2, with a foo_id of 2 is deleted, then the row with id=2 in the table foo is automatically deleted

d.If a row with id = 2 in table foo is deleted, all rows with foo_id = 2 in table foo2 are deleted

In my example I would have used the delete syntax:

DELETE FROM foo WHERE id = 2; DELETE FROM foo2 WHERE foo_id = 2; 

For some reason I was unable to find any relationship between the tables although it seems like there should be one. Maybe there is some MySQL setting or perhaps is ON DELETE CASCADE not used properly in the table creation queries? I am left wondering...

like image 808
Eae Avatar asked Aug 29 '12 20:08

Eae


People also ask

How do I use cascade delete in MySQL?

Use the ON DELETE CASCADE option to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behavior of the database server prevents you from deleting data in a table if other tables reference it.

Does MySQL support cascade delete?

ON DELETE CASCADE clause in MySQL is used to automatically remove the matching records from the child table when we delete the rows from the parent table. It is a kind of referential action related to the foreign key.

Is on delete cascade good?

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.

How does Cascade on delete work?

A foreign key with cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted. This is called a cascade delete in SQL Server.


1 Answers

Answer d. is correct, if and only if the storage engine actually supports and enforces foreign key constraints.

If the tables are created with Engine=MyISAM, then neither b. or d. is correct.

If the tables are created with Engine=InnoDB, then d. is correct.

NOTE:

This is true for InnoDB if and only if FOREIGN_KEY_CHECKS = 1; if FOREIGN_KEY_CHECKS = 0, then a DELETE from the parent table (foo) will not remove rows from the child table (foo2) that reference a row removed from the parent table.

Verify this with the output from SHOW VARIABLES LIKE 'foreign_key_checks' (1=ON, 0=OFF) (The normal default is for this to be ON.)

The output from SHOW CREATE TABLE foo will show which engine the table uses.

The output from SHOW VARIABLES LIKE 'storage_engine' will show the default engine used when a table is created and the engine is not specified.

like image 154
spencer7593 Avatar answered Sep 21 '22 17:09

spencer7593