gpt4 book ai didi

mysql - 如何向有外键的表中插入数据?

转载 作者:行者123 更新时间:2023-11-29 16:58:14 25 4
gpt4 key购买 nike

我有 2 张 table :

                 usine 
======================================
id_usine | nom_usine | referance_usine

对于每个usine,我们都有ligneproduct,所以我还有另一个表:

  ligneproduction
===================
id | nom | fkUsine

我使用此在表中添加了一个项目:

INSERT INTO `USINE` (`id_usine`, `nom_usine`, `referance_usine`)
VALUES ('3', 'LAFARGE BISKRA', 'LAFARGE_BISKRA');

我想在 ligneproduct 表中添加与 usine 表中该项目相对应的“ligne Production”。

我怎样才能做到这一点?

最佳答案

使用这个...设置最后插入的id并插入到子表中并使ligneproduct的id自动递增

INSERT INTO `USINE` (`id_usine`, `nom_usine`, `referance_usine`)
VALUES ('3', 'LAFARGE BISKRA', 'LAFARGE_BISKRA');

SET @id_usine = (SELECT LAST_INSERT_ID());

INSERT INTO `ligneproduction` (`nom`, `fkUsine`)
VALUES ('any value depends on u', @id_usine);

关于mysql - 如何向有外键的表中插入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52421416/

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