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


  #1064-您的SQL语法有误;检查手册
  对应于您的MySQL服务器版本以使用正确的语法
  在“外键”附近
  
  id_livreur int外键,
  
  id_plat int外键,
  
  id_dessert'在第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;

09-30 15:46
查看更多