Mysql无法添加外键约束错误

时间:2017-03-24 09:25:52

标签: mysql sql

我知道这个错误已有几个答案。

我的错误: 1215无法添加外键约束错误

但他们中的任何一个都有效:

- 外键与原始键的类型相同。

- 他们有相同的编码

-all与innoDB

当我做一个SHOW ENGINE INNODB状态时; :

InnoDB=====================================2017-03-23 ....

这是我的代码:

CREATE TABLE IF NOT EXISTS `TBL_USERS_EK` (
  `user_ID_EK` INT(11) NOT NULL AUTO_INCREMENT,
  `user_Name_EK` VARCHAR(100) NOT NULL,
  `user_Email_EK` VARCHAR(100) NOT NULL,
  `user_Pass_EK` VARCHAR(100) NOT NULL,
  `user_Status_EK` enum('Y','N') NOT NULL DEFAULT 'N',
  `tokenCode_EK` VARCHAR(100) NOT NULL,
  `user_rank_EK` VARCHAR(100) NOT NULL DEFAULT 'Apprenti detective',
  `nb_validation_enigma` int(11) NOT NULL DEFAULT 0,
  PRIMARY KEY (`user_ID_EK`),
  UNIQUE KEY `user_Email_EK` (`user_Email_EK`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `TBL_ENIGMA_EK` (
  `enigma_ID_EK` INT(11) NOT NULL AUTO_INCREMENT,
  `nbr_enigma_part_EK` INT(11) NOT NULL,
  `user_ID_EK` INT(11) NOT NULL,
  `validation_status` enum('Y','N') NOT NULL DEFAULT 'N',
  PRIMARY KEY (`enigma_ID_EK`),
  CONSTRAINT `TBL_ENIGMA_FK` FOREIGN KEY (`user_ID_EK`) 
  REFERENCES `TBL_USERS_EK`(`user_ID_EK`) 
  ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `TBL_PART_EK` (
  `part_ID_EK` INT(11) NOT NULL AUTO_INCREMENT,
  `nbr_enigma_subpart_EK` INT(11) NOT NULL,
  `enigma_ID_EK` INT(11) NOT NULL,
  `user_ID_EK` INT(11) NOT NULL,
  `validation_status` enum('Y','N') NOT NULL DEFAULT 'N',
  PRIMARY KEY (`part_ID_EK`),
  CONSTRAINT `TBL_PART_FK1` FOREIGN KEY (`enigma_ID_EK`)
  REFERENCES `TBL_ENIGMA_EK`(`enigma_ID_EK`)
  ON DELETE CASCADE,
  CONSTRAINT `TBL_PART_FK2` FOREIGN KEY (`user_ID_EK`)
  REFERENCES `TBL_USER_EK`(`user_ID_EK`)
  ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

2 个答案:

答案 0 :(得分:3)

TBL_PART_EK到TBL_USERS_EK中的外键存在语法错误。 您已将其名称命名为TBL_USER_EK,而应将其命名为TBL_USERS_EK。

删除错误或重复使用它,它将起作用。

在TBL_PART_EK

CONSTRAINT B<T>外键(TBL_PART_FK2) 参考user_ID_EKTBL_USER_EK) ON DELETE CASCADE

答案 1 :(得分:0)

table FOREIGN KEY语法中有一个拼写错误。它应该是REFERENCES (TBL_USERS_EK而不是REFERENCES (TBL_USER_EK,下面应该有效:

CREATE TABLE IF NOT EXISTS `TBL_PART_EK` (
  `part_ID_EK` INT(11) NOT NULL AUTO_INCREMENT,
  `nbr_enigma_subpart_EK` INT(11) NOT NULL,
  `enigma_ID_EK` INT(11) NOT NULL,
  `user_ID_EK` INT(11) NOT NULL,
  `validation_status` enum('Y','N') NOT NULL DEFAULT 'N',
  PRIMARY KEY (`part_ID_EK`),
  CONSTRAINT `TBL_PART_FK1` FOREIGN KEY (`enigma_ID_EK`)
  REFERENCES `TBL_ENIGMA_EK`(`enigma_ID_EK`)
  ON DELETE CASCADE,
  CONSTRAINT `TBL_PART_FK2` FOREIGN KEY (`user_ID_EK`)
  REFERENCES `TBL_USERS_EK`(`user_ID_EK`)
  ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;