gpt4 book ai didi

mysql - 重启 MySQL 后的 LAST_INSERT_ID

转载 作者:行者123 更新时间:2023-11-29 01:17:04 25 4
gpt4 key购买 nike

我尝试在插入新记录后通过以下语句检索 LAST_INSERT_ID,它工作正常。

SELECT LAST_INSERT_ID();

问题

当在表中插入一条新记录并重新启动 MySQL 服务器,然后尝试检索 LAST_INSERT_ID 时,它得到 0 值!

LAST_INSERT_ID 仅在重启 MySQL 或重启系统之前有效?

如有任何帮助,我们将不胜感激。

最佳答案

MySQL Docs 中所述,

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.

这意味着 - 在您的语句集重新启动后,没有影响 AUTO_INCREMENT 值的语句,因此该值未定义。

关于mysql - 重启 MySQL 后的 LAST_INSERT_ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17651535/

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