gpt4 book ai didi

mysql - 错误: Cannot add foreign key constraint on CREATE TABLE

转载 作者:行者123 更新时间:2023-11-29 13:22:53 25 4
gpt4 key购买 nike

当我运行此命令时,出现错误:无法添加外键约束谁能帮我?谢谢

CREATE SCHEMA `Rubik's_Cubes`;
USE `Rubik's_Cubes`;

/*Tabella Collezionista*/
CREATE TABLE Collezionista(
id INTEGER auto_increment PRIMARY KEY,
nome TEXT NOT NULL,
cognome TEXT NOT NULL,
telefono INTEGER NOT NULL,
ranking INTEGER NOT NULL UNIQUE,
id_ogg_pref INTEGER DEFAULT NULL,

FOREIGN KEY (id_ogg_pref) REFERENCES Oggetto(id)
ON DELETE SET NULL
ON UPDATE CASCADE
);

/*Tabella Possiede*/
CREATE TABLE Possiede(
id_collezionatore INTEGER NOT NULL,
id_oggetto INTEGER NOT NULL,
tempo_sol DOUBLE DEFAULT NULL,

FOREIGN KEY (id_collezionatore) REFERENCES Collezionista(id)
ON DELETE CASCADE
ON UPDATE NO ACTION,

FOREIGN KEY (id_oggetto) REFERENCES Oggetto(id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);

/*Tabella Oggetto*/
CREATE TABLE Oggetto(
id INTEGER AUTO_INCREMENT PRIMARY KEY,
nome TEXT NOT NULL,
id_tipo INTEGER NOT NULL,
id_tipo2 INTEGER DEFAULT NULL,
valore DOUBLE NOT NULL,
descrizione TEXT NOT NULL,
detentore_record INTEGER DEFAULT NULL,
tempo_record DOUBLE DEFAULT NULL,

FOREIGN KEY (id_tipo) REFERENCES Tipologia(id)
ON DELETE NO ACTION
ON UPDATE CASCADE,

FOREIGN KEY (id_tipo2) REFERENCES Tipologia(id)
ON DELETE NO ACTION
ON UPDATE CASCADE,

FOREIGN KEY (detentore_record) REFERENCES Collezionista(id)
ON DELETE SET NULL
ON UPDATE CASCADE
);

/*Tabella Tipologia*/
CREATE TABLE Tipologia(
id INTEGER AUTO_INCREMENT PRIMARY KEY,
nome TEXT NOT NULL,
descrizione TEXT NOT NULL,
provenienza TEXT NOT NULL
);

最佳答案

您尝试添加与尚不存在的表相关的约束。

您应该在创建表语句中删除此内容:

FOREIGN KEY (id_ogg_pref) REFERENCES Oggetto(id)
ON DELETE SET NULL
ON UPDATE CASCADE

然后在末尾添加一个 alter table 。请参阅第一个表的示例:

/*Tabella Collezionista*/
ALTER TABLE Collezionista
ADD FOREIGN KEY (id_ogg_pref) REFERENCES Oggetto(id)
ON DELETE SET NULL
ON UPDATE CASCADE;

关于mysql - 错误: Cannot add foreign key constraint on CREATE TABLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20549393/

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