我在将外键添加到vessel_id时遇到问题。错误和代码如下:
错误:
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (
qubqmlcs_db .
#sql-5ec_1c , CONSTRAINT
FK_boatlogs_vessels_id FOREIGN KEY (
vessel_id ) REFERENCES
船只(
vessel_id ) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL语句:
ALTER TABLE `qubqmlcs_db`.`boatlogs`
ADD INDEX `FK_boatlogs_vessels_id_idx` (`vessel_id` ASC);
;
ALTER TABLE `qubqmlcs_db`.`boatlogs`
ADD CONSTRAINT `FK_boatlogs_vessels_id`
FOREIGN KEY (`vessel_id`)
REFERENCES `qubqmlcs_db`.`vessels` (`vessel_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTIONACTION;
这是我要添加FK键的表:
这是引用的表:
以下是索引:
这是外键:
为什么会出现此错误?我只是不明白。我试图删除vessel_id并再次创建它,然后添加FK密钥,但是每次都会出现此错误。非常感谢您的帮助!