如何处理mysql表中的并发插入并获取正确的插入ID [英] how do i handle concurrent inserts in mysql table and fetch the correct insert id

查看:167
本文介绍了如何处理mysql表中的并发插入并获取正确的插入ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将adodb用于PHP库。

I am using adodb for PHP library.

要获取插入记录的ID,请使用此功能

For fetching the id at which the record is inserted I use this function

"$db->Insert_ID()"

我想知道数据库表中是否同时存在多个插入项,该方法是否可以返回正确的信息?为每个插入的记录插入ID?

I want to know if there are multiple and simultaneous inserts into the database table, will this method return me the correct inserted id for each record inserted ?

我之所以问这个原因是因为我使用最后一个插入ID来进一步处理其他记录并在其中进行后续输入相关表。

The reason I am asking this is because I use this last insert id for further processing of other records and making subsequent entries in the related tables.

这种方法是否足够安全?或者我遗漏了一些东西。

Is this approach safe enough or am I missing something.

请帮助我制定一个适当的工作计划,以便我可以使用最后一个插入ID将其安全地进一步插入另一个表中,而不必弄乱现有数据。

Please help me formulate a proper working plan for this so that I can use the last insert id for further inserts into the other table safely without having to mess up with the existing data.

谢谢

推荐答案

是的,可以安全地同时使用。这是因为 LAST_INSERT_ID()是按连接的,如此处

Yes, it's safe for concurent use. That's because LAST_INSERT_ID() is per-connection, as explained here:


生成的ID为
以每次连接
的价格维护在服务器中。这意味着该函数返回给给定客户端
的值
是为该客户端影响
AUTO_INCREMENT列的
最近语句生成的第一个
AUTO_INCREMENT值。
该值不受其他
客户的影响,即使它们自己生成了
AUTO_INCREMENT值也是如此。
此行为可确保每个客户
都可以检索自己的ID,而无需
担心其他
客户的活动,并且不需要
锁或事务。 / p>

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表中的并发插入并获取正确的插入ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆