gpt4 book ai didi

mysql - 使用 mysql 将值插入带有外键的表中

转载 作者:行者123 更新时间:2023-11-29 19:26:30 25 4
gpt4 key购买 nike

我想在包含外键的表中插入值,但是当我手动添加外键时(例如,通过写入外键的编号来手动添加外键的 id),它不起作用,我的语法错误的错误。

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FOREIGN KEY,

id_livreur int FOREIGN KEY,

id_plat int FOREIGN KEY,

id_dessert ' at line 4

这是我包含外键的表:

CREATE TABLE Commande
(
id_commande int NOT NULL AUTO_INCREMENT PRIMARY KEY,
id_client int FOREIGN KEY,
id_livreur int FOREIGN KEY,
id_plat int FOREIGN KEY,
id_dessert int FOREIGN KEY ,
prix_total int,
heure_estime time,
FOREIGN KEY (id_client) REFERENCES Client(id_client),
FOREIGN KEY (id_livreur) REFERENCES Livreur(id_livreur),
FOREIGN KEY (id_plat) REFERENCES Plat(id_plat),
FOREIGN KEY (id_dessert) REFERENCES Dessert(id_dessert)
) ENGINE=InnoDB;

这是我的插入内容:

INSERT INTO Commande VALUES (1,1,1,1,1,45,now() + INTERVAL 20 minute);
INSERT INTO Commande VALUES (2,2,2,2,2,55,now() + INTERVAL 20 minute);
INSERT INTO Commande VALUES (3,3,3,3,3,75,now() + INTERVAL 20 minute);
INSERT INTO Commande VALUES (4,4,4,4,4,45,now() + INTERVAL 20 minute);

最佳答案

删除列定义后的“FOREIGN KEY”

CREATE TABLE Commande
(
id_commande int NOT NULL AUTO_INCREMENT PRIMARY KEY,
id_client int,
id_livreur int,
id_plat int ,
id_dessert int,
prix_total int,
heure_estime time,
FOREIGN KEY (id_client) REFERENCES Client(id_client),
FOREIGN KEY (id_livreur) REFERENCES Livreur(id_livreur),
FOREIGN KEY (id_plat) REFERENCES Plat(id_plat),
FOREIGN KEY (id_dessert) REFERENCES Dessert(id_dessert)
) ENGINE=InnoDB;

关于mysql - 使用 mysql 将值插入带有外键的表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42092333/

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