并发情况下,向mysql的两张表插入数据,B是A关联表

MySql 码拜 7年前 (2017-05-07) 2480次浏览
两张表A、B,两张表都有本人的自增主键id,B表中有A表的外键,A与B的关系是一对多;
现在是多用户同时向这两张表中插入数据。
本人想在插入A表信息的时候马上获得A的主键,last_insert_id()这个方法在并发的情况下获取的不一定是本人本人insert的id吧。
有没有什么比较高效的方法?
解决方案

40

引用

The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.

并发情况下,向mysql的两张表插入数据,B是A关联表


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明并发情况下,向mysql的两张表插入数据,B是A关联表
喜欢 (0)
[1034331897@qq.com]
分享 (0)