无法在MySQL中写入重复键

时间:2017-01-21 16:16:24

标签: mysql duplicates

Executing SQL script in server
ERROR: Error 1022: Can't write; duplicate key in table 'dependent'
SQL Code:
        -- -----------------------------------------------------
        -- Table `mydb`.`DEPENDENT`
        -- -----------------------------------------------------


 CREATE TABLE IF NOT EXISTS `mydb`.`DEPENDENT` (
          `Essn` CHAR(9) NOT NULL,
          `Dependent_name` VARCHAR(45) NOT NULL,
          `Sex` CHAR NULL,
          `Bdate` DATE NULL,
          `Relationship` VARCHAR(45) NULL,
          PRIMARY KEY (`Dependent_name`, `Essn`),
          CONSTRAINT `Essn`
            FOREIGN KEY (`Essn`)
            REFERENCES `mydb`.`EMPLOYEE` (`Ssn`)
            ON DELETE NO ACTION
            ON UPDATE NO ACTION)
        ENGINE = InnoDB
  

SQL脚本执行完成:语句:10个成功,1个失败

Fetching back view definitions in final form.
Nothing to fetch`enter code here`

我收到此错误。有人可以帮我这个。感谢

1 个答案:

答案 0 :(得分:0)

您在Dependent_name上定义了主键,如果您尝试插入相同的名称2次,则会出现错误因此,如果您希望重复名称使用自动增量列作为主键,则必须定义其他主键但您也可以为主键定义复合键