簡體   English   中英

如何將數據插入具有外鍵的表中?

[英]how to insert data into a table that has a foreign key?

我有2張桌子:

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

對於每個usine我們都有ligneproduction所以我有另一個表:

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

我在表中添加了一項:

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

我想在ligneproduction表中添加與usine表中該項目相對應的“ ligne production”。

我怎樣才能做到這一點?

使用此...設置最后插入的ID並插入子表中,並使ligneproduction的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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM