Finding the next available id in MySQL(在 MySQL 中查找下一个可用的 id)
问题描述
我必须在 MySQL 数据库中找到下一个可用的 id(如果数据库中有 5 个数据,我必须获取下一个可用的插入位置,即 6).我怎样才能做到这一点?我已经使用了 MAX(id)
,但是当我从数据库中删除一些行时,它仍然保留它没有更新的旧最大值.
I have to find the next available id (if there are 5 data in database, I have to get the next available insert place which is 6) in a MySQL database. How can I do that?
I have used MAX(id)
, but when I delete some rows from the database, it still holds the old max value it didn't update.
推荐答案
我认为您永远无法确定 next id,因为有人可能会在您询问之后插入新行对于下一个 ID.你至少需要一个事务,如果我没记错的话,你只能在插入它之后获得实际使用的 id,至少这是处理它的常用方法——见 http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
I don't think you can ever be sure on the next id, because someone might insert a new row just after you asked for the next id. You would at least need a transaction, and if I'm not mistaken you can only get the actual id used after inserting it, at least that is the common way of handling it -- see http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
这篇关于在 MySQL 中查找下一个可用的 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 MySQL 中查找下一个可用的 id


- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- SQL 临时表问题 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01