在MySQL中查找下一个可用的ID [英] Finding the next available id in MySQL
问题描述
我必须在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,至少这是处理该ID的常用方法-请参见 http://dev.mysql.com/doc/refman/5.0/zh/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!