gpt4 book ai didi

mysql - 错误 1215 : cannot add foreign key contraint

转载 作者:行者123 更新时间:2023-11-29 21:52:42 25 4
gpt4 key购买 nike

我得到了

错误 1215:无法在我的 sql 脚本上添加外键约束。

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

嗯,我的外键类型相同,但出现此错误。

这是我的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;

最佳答案

我不知道你为什么会出错,但在我的中,没关系,日志如下:

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

关于mysql - 错误 1215 : cannot add foreign key contraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33476995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com