mysql无法写 - 重复密钥

时间:2014-05-22 20:56:35

标签: mysql

我不明白。有人可以在这里解释我的失败:

CREATE TABLE IF NOT EXISTS `mytable`.`Employee_Service` (
  `Employee_Service_Id` INT NOT NULL,
  `Created_At` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  `Updated_At` TIMESTAMP NULL,
  PRIMARY KEY (`Employee_Service_Id`),
  CONSTRAINT `Employee`
    FOREIGN KEY (`Employee_Service_Id`)
    REFERENCES `mytable`.`Employees` (`Employee_Id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `Service`
    FOREIGN KEY (`Employee_Service_Id`)
    REFERENCES `mytable`.`Services` (`Service_Id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB

日志文件说:

Error 1022: Can't write; duplicate key in table 'employee_service'

要了解我想用它做什么: 表" Employee_Service"表格和#34;员工之间的联系"和"服务"。在该表中,我想存储" Employee_Id"从表格"员工"并将其与" Service_Id"从表格"服务"。所以我可以得到员工有服务的关系。 但是我的桌子出了什么问题,尤其是外出钥匙???

1 个答案:

答案 0 :(得分:1)

很可能您的其他表具有名为EmployeeService的相同约束。您发布的CREATE语句的另一个问题是,您尝试将Employee_Service_Id同为PKFK

PRIMARY KEY (`Employee_Service_Id`),
  CONSTRAINT `Employee`
    FOREIGN KEY (`Employee_Service_Id`)