302Laya
302Laya

Reputation: 123

The Foreign Key Options, ON DELETE CASCADE not working?

I am trying to use ON DELETE CASCADE for a database I'm working on. Didn't seem to work so I tested it out on a simple example with no success.

CREATE TABLE foo (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
data VARCHAR(10),
PRIMARY KEY (id)
)ENGINE=InnoDB;

CREATE TABLE foo2 (
id INT UNSIGNED NOT NULL,
data2 VARCHAR(10),
PRIMARY KEY (id),
CONSTRAINT fk_foo2_id FOREIGN KEY (id) REFERENCES foo(id) ON DELETE CASCADE
)ENGINE=InnoDB;

INSERT INTO foo (data) VALUE ('hello'),('world'),('mysql');
INSERT INTO foo2 (data2) VALUE ('hello2'),('world2'),('mysql2');

SELECT * FROM foo;

+----+-------+
| id | data  |
+----+-------+
|  1 | hello |
|  2 | world |
|  3 | mysql |
+----+-------+
3 rows in set (0.00 sec)

SELECT * FROM foo2;

+----+--------+
| id | data2  |
+----+--------+
|  1 | hello2 |
|  2 | world2 |
|  3 | mysql2 |
+----+--------+
3 rows in set (0.00 sec)

DELETE FROM foo WHERE id=2;

SELECT * FROM foo;
+----+-------+
| id | data  |
+----+-------+
|  1 | hello |
|  3 | mysql |
+----+-------+
2 rows in set (0.00 sec)

SELECT * FROM foo2;

+----+--------+
| id | data2  |
+----+--------+
|  1 | hello2 |
|  2 | world2 |
|  3 | mysql2 |
+----+--------+
3 rows in set (0.00 sec)

I can't for the life of me figure out why this isn't working. I looked at similar questions and answers on here and I did exactly what they said and it still didn't work. Most of them just said to change to ENGINE=InnoDb, but I tried it and no success.

There must be something I'm missing here, and it's probably very obvious.. Monday mornings.

If anyone can shed some light on this little noob problem of mine, I would greatly appreciate it!

Edit: removed the auto_increment from id in foo2 as it did not belong there

Upvotes: 1

Views: 4543

Answers (2)

spencer7593
spencer7593

Reputation: 108380

The first thing that pops to mind is to check the setting of the foreign_key_checks variable. If that's set to 0 (FALSE), then foreign key constraints are NOT enforced.

SHOW VARIABLES LIKE 'foreign_key_checks'

To enable foeign key constraints, set to the variable to 1

SET foreign_key_checks = 1;

NOTE: this affects only the current session. New sessions inherit the GLOBAL setting.


Also, verify that your tables are actually using the InnoDB engine, and that the foreign keys are defined. Easiest way is to get the output from:

SHOW CREATE TABLE foo;
SHOW CREATE TABLE foo2;

FOLLOWUP

This is something that we expect NOT to be broken in MySQL 5.1.61.

As a workaround, try defining the foreign key constraint as a separate ALTER TABLE statement.

ALTER TABLE foo2 
ADD CONSTRAINT fk_foo2_id FOREIGN KEY (id) REFERENCES foo(id) ON DELETE CASCADE ;

Upvotes: 9

Jörg Brühe
Jörg Brühe

Reputation: 66

I don't see much use in a foreign key constraint between two columns that are both defined with "auto_increment". In your example, you could easily create several rows in table "foo" (without a counterpart in "foo2"), and from then onwards you could not control whether "id" values in both tables match.

I admit I didn't check the documentation, but it would not surprise me if MySQL silently ignored a foreign key constraint for an auto-generated column.

IMNSHO, your table "foo2" should use "id" values which are set explicitly and reference specific rows in "foo", because then it would make sense that deleting such "foo" rows should cascade onto "foo2".

Upvotes: 2

Related Questions