简体   繁体   English

MySQL级联删除不起作用

[英]Mysql cascade delete not working

In mysql, I have two tables players and walls . 在mysql中,我有两个表playerswalls

I use this to create the walls table: 我用它来创建墙表:

CREATE TABLE walls (id int AUTO_INCREMENT, player_id int, x int, y int, PRIMARY KEY(id), FOREIGN KEY (player_id) REFERENCES players(id) ON DELETE CASCADE);

alter table walls add unique index(x, y);

The column player_id is a foreign key to the id column of the players table. player_idplayers表的id列的外键。 I want it so that, if any rows gets deleted from the players table, then any rows from the walls table that have a foreign key to the deleted rows of the players table, should also get deleted from the walls table automatically using cascade delete. 我希望这样,如果从players表中删除了任何行,那么walls表中具有对players表的已删除行具有外键的任何行,也应该使用级联删除从walls表中自动删除。 But when I do this, the rows in the walls table don't get deleted. 但是当我这样做时,walls表中的行不会被删除。

Anyone know whats wrong here? 有人知道这里有什么问题吗?

Thanks 谢谢

Confirm that your tables are actually using an engine which supports foreign keys, eg InnoDB. 确认您的表实际上使用的是支持外键的引擎,例如InnoDB。 Older MySQLs will by default create MyISAM tables, which understand the foreign key definitions, but completely ignore them. 较早的MySQL默认情况下会创建MyISAM表,该表理解外键定义,但完全忽略它们。

Simple test: show create table yourtable; 简单测试: show create table yourtable; . If the engine= line indicates it's a non-FK-enabled engine, there's your problem. 如果engine=行表明它不是启用FK的引擎,则说明您有问题。

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

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