gpt4 book ai didi

mysql - mySQL 中的并发插入

转载 作者:搜寻专家 更新时间:2023-10-30 20:08:30 25 4
gpt4 key购买 nike

我有 3 个表 - TB1、TB2 和 r_tb1_tb2 (innoDB)

TB1 持有用户的详细信息(将被插入)
- id(主要的,唯一的)
- 姓名

TB2 包含用户可以参加的类(class)的详细信息(静态表)
- id(主要的,唯一的)
- 类(class)名称

r_tb1_tb2 保存两个表之间的关系
- rID
- user_id(来自表 1)
- course_id(引用表2)

当我在 TB1 中插入一个新行时,我得到最后插入行的 ID。并使用它在 r_tb1_tb2 中插入另一行

我可以预见,在 tb1 中同时插入实例的情况下,这可能会导致错误的条目。

有人可以指出此类同步更新的最佳做法吗。

提前致谢。

最佳答案

last_insert_id为此内置了保护

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.

(强调他们的)

因此,如果两个不同的用户在您的站点上执行导致记录插入到 T1 中的操作,则这些用户的 last_insert_id 将不同,因为他们使用两个不同的连接(客户端上文)

关于mysql - mySQL 中的并发插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38540203/

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