简体   繁体   中英

Mysql Cannot add foreign key constraint error

I know this error already has several answers.

My error : 1215 Cannot add foreign key constraint error

But any of them works :

-The foreign keys have the same type as the original keys.

-they have the same encoding

-all are with innoDB

When I do a SHOW ENGINE INNODB STATUS; :

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

Here is my code :

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 ;

There is syntax error for foreign key in TBL_PART_EK to TBL_USERS_EK. You have given name of it as TBL_USER_EK instead it should be TBL_USERS_EK.

Remove Typo or replcae with this ,it will work.

in TBL_PART_EK

CONSTRAINT TBL_PART_FK2 FOREIGN KEY ( user_ID_EK ) REFERENCES TBL_USER_EK ( user_ID_EK ) ON DELETE CASCADE

There is a typo in the table name of FOREIGN KEY syntax. It should be REFERENCES (TBL_USERS_EK and not REFERENCES (TBL_USER_EK , below should work:

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 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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