create语句上的MySQL外键子句导致连接错误丢失

时间:2015-06-22 13:42:24

标签: mysql

我正在运行MySQL 5.5.43-0 + deb8u1并尝试创建下表:

CREATE TABLE IF NOT EXISTS `car_favorites` (  
  `id` INTEGER UNSIGNED auto_increment ,  
  `user_id` INTEGER UNSIGNED NOT NULL,  
  `car_id` INTEGER UNSIGNED NOT NULL,  
  `created_at` DATETIME NOT NULL,  
  `updated_at` DATETIME NOT NULL,  
  `deleted_at` DATETIME,   
  UNIQUE `user_id_car_id` (`user_id`, `car_id`),   
  PRIMARY KEY (`id`),   
  FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,   
  FOREIGN KEY (`car_id`) REFERENCES `cars` (`id`) ON DELETE SET NULL ON UPDATE CASCADE   
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;

引用的表userscars已经存在。当我尝试运行此查询时,出现以下错误:

ERROR 2013 (HY000): Lost connection to MySQL server during query

之后,在执行SHOW TABLES之类的操作时会列出该表,但如果我尝试对表执行查询(即DESCRIBE car_favorites),则服务会报告该表不存在,并重新启动服务使表格消失。

但是,当我删除这部分查询时:

FOREIGN KEY (`car_id`) REFERENCES `cars` (`id`) ON DELETE SET NULL ON UPDATE CASCADE

创建表没有任何问题。奇怪的是,如果我运行此查询:

SHOW ENGINE INNODB STATUS;

在运行失败的创建查询后立即显示或未列出任何外键错误。打开MySQL日志记录并检查错误日志会显示这个相当模糊的错误:

13:21:41 UTC - mysqld got signal 11 ;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.
We will try our best to scrape up some info that will hopefully help
diagnose the problem, but since we have already crashed,
something is definitely wrong and this may fail.

其次是堆栈跟踪和其他一些信息,但没有具体信息。

我不知道为什么这个外键子句导致了这次崩溃。任何有关正确方向的见解或观点都将受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

通过写这个问题的过程,我找到了答案。如果你真的喘不过气来阅读创建查询,那就相当明显了。

由于我使用car_id声明了列NOT NULL,因此外键声明的ON DELETE SET NULL部分没有任何意义。将该子句更改为ON DELETE CASCADE解决了问题。

为什么MySQL窒息而且不能给我一个更具体的错误,我不知道。