错误代码:1022。不能写; table6中的重复键

时间:2016-12-12 13:37:12

标签: mysql

首先,我创建了下表没有任何问题:

CREATE TABLE IF NOT EXISTS `forecast`.`forecast_main_2` (
`ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`data_carga` DATE NOT NULL,
`mes_ref1` DATE NOT NULL,
`mes_ref2` DATE NOT NULL,
`mes_ref3` DATE NULL DEFAULT NULL,
`tipo` VARCHAR(50) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE INDEX `ID_UNIQUE` (`ID` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

但是当我尝试创建下一个时:

CREATE TABLE IF NOT EXISTS `forecast`.`forecast_labs_2` (
`ID_labs` INT(11) UNSIGNED NOT NULL,
`sku` INT(11) NOT NULL,
`prev_m1_labs` DOUBLE NULL DEFAULT NULL,
`prev_m2_labs` DOUBLE NULL DEFAULT NULL,
`prev_m3_labs` DOUBLE NULL DEFAULT NULL,
`forecast_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`ID_labs`, `forecast_id`),
INDEX `forecast_id_UNIQUE` (`ID_labs` ASC),
INDEX `ID_UNIQUE` (`forecast_id` ASC),
CONSTRAINT `ID`
FOREIGN KEY (`forecast_id`)
REFERENCES `forecast`.`forecast_main_2` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

出现以下错误:"错误代码:1022。无法写入;表格中的重复键" forecast_labs_2'"

我真的无法看到重复键的位置。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

谢谢Solarflare,就是这样。在我没有命名约束之后,它工作得很好。很抱歉,如果问题很简单,我仍然是SQL的初学者,但你帮了很多忙!