简体   繁体   English

外键选项,ON DELETE CASCADE不起作用?

[英]The Foreign Key Options, ON DELETE CASCADE not working?

I am trying to use ON DELETE CASCADE for a database I'm working on. 我正在尝试对正在使用的数据库使用ON DELETE CASCADE 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. 他们中的大多数人都只是说要更改为ENGINE=InnoDb ,但是我尝试了但没有成功。

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 编辑:从foo2 id中删除了auto_increment ,因为它不属于那里

The first thing that pops to mind is to check the setting of the foreign_key_checks variable. 我想到的第一件事是检查foreign_key_checks变量的设置。 If that's set to 0 (FALSE), then foreign key constraints are NOT enforced. 如果将其设置为0(FALSE),则不强制使用外键约束。

SHOW VARIABLES LIKE 'foreign_key_checks'

To enable foeign key constraints, set to the variable to 1 要启用功能键约束,请将变量设置为1

SET foreign_key_checks = 1;

NOTE: this affects only the current session. 注意:这仅影响当前会话。 New sessions inherit the GLOBAL setting. 新的会话继承了GLOBAL设置。


Also, verify that your tables are actually using the InnoDB engine, and that the foreign keys are defined. 另外,请验证您的表实际上正在使用InnoDB引擎,并且已定义了外键。 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. 我们希望这不会在MySQL 5.1.61中被打破。

As a workaround, try defining the foreign key constraint as a separate ALTER TABLE statement. 解决方法是,尝试将外键约束定义为单独的ALTER TABLE语句。

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

I don't see much use in a foreign key constraint between two columns that are both defined with "auto_increment". 我在两个都用“ 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. 在您的示例中,您可以轻松地在表“ foo”中创建几行(在“ foo2”中没有对应行),从那时起,您将无法控制两个表中的“ id”值是否匹配。

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. 我承认我没有检查文档,但是如果MySQL默默地忽略了自动生成列的外键约束,这也不会令我感到惊讶。

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". IMNSHO,您的表“ foo2”应使用显式设置的“ id”值,并引用“ foo”中的特定行,因为这样有意义的是,删除此类“ foo”行应级联到“ foo2”上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM