我如何处理 mysql 表中的并发插入并获取正确的插入 id

发布于 2024-08-18 21:51:40 字数 334 浏览 4 评论 0原文

我正在使用 adodb 作为 PHP 库。

为了获取插入记录的 id,我使用此函数

"$db->Insert_ID()"

我想知道数据库表中是否有多个同时插入,此方法是否会为插入的每条记录返回正确的插入 ID?

我问这个问题的原因是因为我使用最后一个插入 ID 来进一步处理其他记录并在相关表中创建后续条目。

这种方法是否足够安全,或者我是否遗漏了某些内容。

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

谢谢

I am using adodb for PHP library.

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

"$db->Insert_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 ?

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.

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.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔戏命师 2024-08-25 21:51:41

是的,并发使用是安全的。这是因为 LAST_INSERT_ID() 是针对每个连接的,如

生成的ID是
维护在服务器上
每个连接的基础。这意味着
函数返回的值
给定的客户是第一个
生成的 AUTO_INCREMENT 值
最近的声明影响
该客户端的 AUTO_INCRMENT 列。
该值不受其他值影响
客户,即使他们产生
自己的 AUTO_INCRMENT 值。
此行为确保每个客户端
可以检索自己的 ID,无需
关心其他人的活动
客户,并且无需
锁或事务。

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

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.

暗喜 2024-08-25 21:51:41

$db->Insert_ID() 只会返回最后插入的 id,因此如果您要插入许多记录并想要获取每个最后插入的行的 id,那么这将成功工作。

The $db->Insert_ID() will return you last insert id only so if you are inserting many records and want to get id of each last inserted row, then this will work successfully.

物价感观 2024-08-25 21:51:41

我想知道数据库表中是否有多个同时插入的内容,此方法是否会为每个插入的记录返回正确的插入 ID?

它将仅返回最近插入的 ID。
为了获取多个插入的 id,您必须在执行每个语句后调用 INSERT_ID()。 IE:

INSERT INTO TABLE ....
INSERT_ID()

INSERT INTO TABLE ....
INSERT_ID()

...获取每个插入的 id 值。如果您运行:

INSERT INTO TABLE ....
INSERT INTO TABLE ....
INSERT_ID()

...将仅返回最后一个插入语句的 id。

这种方法足够安全吗,还是我遗漏了一些东西。

它比使用 SELECT MAX(id) FROM TABLE 更安全,后者有返回由其他人插入的值以及与隔离级别相关的其他内容的风险。

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 ?

It will return only the most recently inserted id.
In order to get ids for multiple inserts, you will have to call INSERT_ID() after each statement is executed. IE:

INSERT INTO TABLE ....
INSERT_ID()

INSERT INTO TABLE ....
INSERT_ID()

...to get the id value for each insert. If you ran:

INSERT INTO TABLE ....
INSERT INTO TABLE ....
INSERT_ID()

...will only return the id for the last insert statement.

Is this approach safe enough or am I missing something.

It's safer than using SELECT MAX(id) FROM TABLE, which risks returning a value inserted by someone else among other things relating to isolation levels.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文