错误1215:无法添加外键约束

时间:2015-11-02 11:53:19

标签: mysql sql database

我正在接受

error 1215cannot add foreign key contraint on my sql script.

我已经检查了有关此问题的其他问题,答案始终是您必须设置相同的类型。

嗯,我的外键有相同的类型,但是我收到了这个错误。

这是我的sql脚本的代码:

CREATE DATABASE IF NOT EXISTS CarLoan;

USE CarLoan;


CREATE TABLE IF NOT EXISTS `CarLoan`.`sede` (
`id_sede` INT NOT NULL,
`nome` VARCHAR(45) NOT NULL,
`citta` VARCHAR(45) NOT NULL,
`via` VARCHAR(45) NOT NULL,
`telefono` VARCHAR(9) NULL,
PRIMARY KEY (`id_sede`))
ENGINE = InnoDB;


CREATE TABLE IF NOT EXISTS `CarLoan`.`impiegato` (
`cf` CHAR(16) NOT NULL,
`nome` VARCHAR(45) NOT NULL,
`cognome` VARCHAR(45) NOT NULL,
`id_sede` INT NOT NULL,
`mail` VARCHAR(45) NOT NULL,
`password` VARCHAR(45) NOT NULL,
PRIMARY KEY (`cf`),
INDEX `id_sede_idx` (`id_sede` ASC),
  FOREIGN KEY (`id_sede`)
REFERENCES `CarLoan`.`sede` (`id_sede`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;

 CREATE TABLE IF NOT EXISTS `CarLoan`.`direttore` (
`cf` CHAR(16) NOT NULL,
`nome` VARCHAR(45) NOT NULL,
`cognome` VARCHAR(45) NOT NULL,
`id_sede` INT NOT NULL,
 `mail` VARCHAR(45) NOT NULL,
`password` VARCHAR(45) NOT NULL,
PRIMARY KEY (`cf`),
INDEX `id_sede_idx` (`id_sede` ASC),
FOREIGN KEY (`id_sede`)
REFERENCES `CarLoan`.`sede` (`id_sede`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;

1 个答案:

答案 0 :(得分:0)

我不知道你为什么会有这个错误,但在我的情况下,没关系,日志如下:

sql> CREATE DATABASE IF NOT EXISTS CarLoan
[2015-11-02 19:59:40] 1 row(s) affected in 6ms


sql> USE CarLoan
[2015-11-02 19:59:40] completed in 3ms


sql> CREATE TABLE IF NOT EXISTS `CarLoan`.`sede` (
  `id_sede` INT NOT NULL,
  `nome` VARCHAR(45) NOT NULL,
  `citta` VARCHAR(45) NOT NULL,
  `via` VARCHAR(45) NOT NULL,
  `telefono` VARCHAR(9) NULL,
  PRIMARY KEY (`id_sede`))
  ENGINE = InnoDB


[2015-11-02 19:59:41] completed in 725ms


sql> CREATE TABLE IF NOT EXISTS `CarLoan`.`impiegato` (
  `cf` CHAR(16) NOT NULL,
  `nome` VARCHAR(45) NOT NULL,
  `cognome` VARCHAR(45) NOT NULL,
  `id_sede` INT NOT NULL,
  `mail` VARCHAR(45) NOT NULL,
  `password` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`cf`),
  INDEX `id_sede_idx` (`id_sede` ASC),
  FOREIGN KEY (`id_sede`)
  REFERENCES `CarLoan`.`sede` (`id_sede`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
  ENGINE = InnoDB


[2015-11-02 19:59:42] completed in 748ms


sql> CREATE TABLE IF NOT EXISTS `CarLoan`.`direttore` (
  `cf` CHAR(16) NOT NULL,
  `nome` VARCHAR(45) NOT NULL,
  `cognome` VARCHAR(45) NOT NULL,
  `id_sede` INT NOT NULL,
  `mail` VARCHAR(45) NOT NULL,
  `password` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`cf`),
  INDEX `id_sede_idx` (`id_sede` ASC),
  FOREIGN KEY (`id_sede`)
  REFERENCES `CarLoan`.`sede` (`id_sede`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
  ENGINE = InnoDB


[2015-11-02 19:59:42] completed in 472ms