dpt62283 2019-03-05 09:11
浏览 101
已采纳

如何为主表和辅助表编写SQL多插入查询

I have two tables and I want to write a multi insert query such that, the PK from first table is auto inserted as fk in another table.

Let us assume following table schemas:

tblParent

ParentID (Primary Key Auto increment)
ParentValue

tblChild

ChildID (Primary Key Auto increment)
FkParentID (Foreign Key)
ChildValue

Now I want to write a multi insert query like following

INSERT INTO tblParent, tblChild (ParentValue, FkParentID, ChildValue) VALUES ('Parent 1', <ParentID of 'Parent 1'>, 'Child 1'), ('Parent 2', <ParentID of 'Parent 2'>, 'Child 2'), ('Parent 3', <ParentID of 'Parent 3'>, 'Child 3')

But I do not know how to do this. I have millions of records to be inserted in these tables. And I do not want to insert a record in first table, then fetch parent id and insert records in second table.

Edit 1: My Database is Mysql InnoDb

Edit 2: The tables in question have one-to-many relationship. So this means I want to perform one insert in tblParent and multiple inserts into tblChild for each record in tblParent

Edit 3:

I was looking in the MySql documentation and came across following description:

https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_last-insert-id

The currently executing statement does not affect the value of LAST_INSERT_ID(). Suppose that you generate an AUTO_INCREMENT value with one statement, and then refer to LAST_INSERT_ID() in a multiple-row INSERT statement that inserts rows into a table with its own AUTO_INCREMENT column. The value of LAST_INSERT_ID() will remain stable in the second statement; its value for the second and later rows is not affected by the earlier row insertions. (However, if you mix references to LAST_INSERT_ID() and LAST_INSERT_ID(expr), the effect is undefined.)

So my next question is, how would LAST_INSERT_ID behave in case of multi insert for tblParent which in turn has multiple inserts in tblChild for each tblParent

  • 写回答

3条回答 默认 最新

  • duanmeng1950 2019-03-06 07:20
    关注

    Anyways, for now what I did is used multi_query to insert values.

    I did following inside a while loop:

    $autoIncForParent = <Max ID from tblParent>;
    while(<condition to evaluate>) {
        if (!empty($multiQuery)) {
            $multiQuery .= ";";
        }
        $multiQuery .= "INSERT INTO tblParent (ParentID , ParentValue) VALUES ($autoIncForParent, 'Parent 1');";
        $multiQuery .= "INSERT INTO tblChild (FkParentID , ChildValue) VALUES ($autoIncForParent, 'Child 1');";
    $autoIncForParent++;
    }
    $mysqli->multi_query($multiQuery);
    

    I am pretty sure there must be another good way of solving this.

    I have taken care of memory issues, race conditions and what nots in my actual code. This snippet is for elaboration purpose only. By no means this snippet is perfect and I would not recommend this for production.

    Edit 1: Added missing semicolons. In case someone refers this

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?