创建外键时出错:mysql错误1215:无法添加外键约束

bogh5gae  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(349)

这个问题在这里已经有答案了

mysql创建带有外键的表,给出errno:150(20个答案)
两年前关门了。
我目前正在为一家公司编码一个预订系统,他们也想要一个任务管理系统,但我遇到了一个让我发疯的错误。

Executing SQL script in server
ERROR: Error 1215: Cannot add foreign key constraint
SQL Code:
        -- -----------------------------------------------------
        -- Table `bvsv_system`.`task`
        -- -----------------------------------------------------
        CREATE TABLE IF NOT EXISTS `bvsv_system`.`task` (
          `idtask` INT(11) NOT NULL,
          `attGora` VARCHAR(200) NULL,
          `status` VARCHAR(45) NULL,
          `to` VARCHAR(45) NULL,
          `jobstatus_id` INT(11) NOT NULL,
          `worker_personnummer` VARCHAR(45) NOT NULL,
          PRIMARY KEY (`idtask`, `jobstatus_id`, `worker_personnummer`),
          INDEX `fk_task_jobstatus1_idx` (`jobstatus_id` ASC),
          INDEX `fk_task_worker1_idx` (`worker_personnummer` ASC),
          CONSTRAINT `fk_task_jobstatus1`
            FOREIGN KEY (`jobstatus_id`)
            REFERENCES `bvsv_system`.`jobstatus` (`id`)
            ON DELETE NO ACTION
            ON UPDATE NO ACTION,
          CONSTRAINT `fk_task_worker1`
            FOREIGN KEY (`worker_personnummer`)
            REFERENCES `bvsv_system`.`worker` (`personnummer`)
            ON DELETE NO ACTION
            ON UPDATE NO ACTION)
        ENGINE = InnoDB

SQL script execution finished: statements: 15 succeeded, 1 failed

当我尝试添加一个链接这两个表的外键时,会出现这个错误

CREATE TABLE IF NOT EXISTS `bvsv_system`.`worker` (
  `personnummer` VARCHAR(45) NOT NULL,
  `fornamn` VARCHAR(45) NULL DEFAULT NULL,
  `efternamn` VARCHAR(45) NULL DEFAULT NULL,
  `extraanstalld` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`personnummer`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;

CREATE TABLE IF NOT EXISTS `bvsv_system`.`task` (
  `idtask` INT(11) NOT NULL,
  `attGora` VARCHAR(200) NULL,
  `status` VARCHAR(45) NULL,
  `to` VARCHAR(45) NULL,
  `jobstatus_id` INT(11) NOT NULL,
  `worker_personnummer` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`idtask`, `jobstatus_id`, `worker_personnummer`),
  INDEX `fk_task_jobstatus1_idx` (`jobstatus_id` ASC),
  INDEX `fk_task_worker1_idx` (`worker_personnummer` ASC),
  CONSTRAINT `fk_task_jobstatus1`
    FOREIGN KEY (`jobstatus_id`)
    REFERENCES `bvsv_system`.`jobstatus` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_task_worker1`
    FOREIGN KEY (`worker_personnummer`)
    REFERENCES `bvsv_system`.`worker` (`personnummer`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

我的同事们会非常感谢你们的帮助!:)

whlutmcx

whlutmcx1#

外键和主键的定义必须完全相同。这个 worker 表定义 DEFAULT CHARACTER SET = latin1 .
我不知道你为什么要为不同的表使用不同的字符集。我建议只使用数据库默认值。
如果您确实需要它们,请使字符集兼容。可以在列级别执行此操作:

CREATE TABLE IF NOT EXISTS `task` (
  `idtask` INT(11) NOT NULL,
  `attGora` VARCHAR(200) NULL,
  `status` VARCHAR(45) NULL,
  `to` VARCHAR(45) NULL,
  `jobstatus_id` INT(11) NOT NULL,
  `worker_personnummer` VARCHAR(45) CHARACTER SET latin1 NOT NULL,
  PRIMARY KEY (`idtask`, `jobstatus_id`, `worker_personnummer`),
  INDEX `fk_task_jobstatus1_idx` (`jobstatus_id` ASC),
  INDEX `fk_task_worker1_idx` (`worker_personnummer` ASC),
  CONSTRAINT `fk_task_worker1`
    FOREIGN KEY (`worker_personnummer`)
    REFERENCES `worker` (`personnummer`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION
)
ENGINE = InnoDB

相关问题