简体   繁体   English

使用mysql使用外键将值插入表中

[英]Insert values into a table with a foreign key using mysql

I want to insert values in a table containing foreign keys, but when I add the foreign keys manually (for example, addin the id of the foreign key manually by writing it's number), it doesn't work and it me the error of a wrong syntax. 我想在包含外键的表中插入值,但是当我手动添加外键时(例如,通过写数字手动添加外键的ID),它不起作用,这是我的错误错误的语法。

#1064 - You have an error in your SQL syntax; #1064-您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FOREIGN KEY, 查看与您的MySQL服务器版本相对应的手册,以在'FOREIGN KEY'附近使用正确的语法,

id_livreur int FOREIGN KEY, id_livreur int外键,

id_plat int FOREIGN KEY, id_plat int外键,

id_dessert ' at line 4 id_dessert'在第4行

Here is my table that contains foreign keys : 这是我的包含外键的表:

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;

And here are my inserts : 这是我的插入内容:

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);

remove "FOREIGN KEY" after the column definition 在列定义后删除“ 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM