如何重用auto_increment值? [英] How to reuse auto_increment values?

查看:149
本文介绍了如何重用auto_increment值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在mysql中对id(auto_increment列)的碎片


我在我的数据库中有此列。让我们说它的名字是'threadid'。
它包含给每个线程唯一的id以供区分。



threadid
9
8
7
6
5
4
3
2
1



假设我已删除ID为5的主题和6。



threadid
9
8
7
4
3
2
1



但是,当删除后有一个提交,给该线程的唯一的id是10.不是5.我认为这不是整洁。 / p>

如何获取列中最小的值? (在这种情况下,5。)



我想我可以使用MIN()获得列的最小值,并保持+1,直到获得未使用的唯一值,但我认为太复杂了。



有什么简单的方法吗?



/ p>

解决方案

我同意大家的其他人,这是一个非常糟糕的主意来实现自己的最小开放号码。但是在我工作的地方,我们给出了一组封闭的数字,当一个数字释放时,我们必须重用。



这是我们做的。 p>

我们不删除行,而是将每一列的所有值设置为null,所以你所做的是
SELECT min(id)WHERE columnA IS NULL,if这个返回的东西,我们重用,否则,插入一个新行。


Possible Duplicate:
Fragmentation of id's (auto_increment column) in mysql

I have this column in my database. Let's say its name is 'threadid'. It contains unique ids given to each thread for distinction.

threadid 9 8 7 6 5 4 3 2 1

Let's say I have deleted threads with id 5 and 6.

threadid 9 8 7 4 3 2 1

But when there is a submission after the deletion, the unique id given to that thread is 10. not 5. I think this is not neat.

How do I get the least possible value in the column? (in this case, 5.)

I think I can get the minimum value of the column using MIN() and keep +1 until I get the unused, unique value, but I think that's too complicated.

Is there any simple way to do this?

Thanks.

解决方案

I agree with the rest of everyone where it is a very bad idea to implement your own find minimal open number. But at where I work, we are given a closed set of number and when a number is free up, we must reuse.

Here is how we did it.

We do not delete the row, but set all values of every column null, So what you do is SELECT min(id) WHERE columnA IS NULL, and if this returns something, we reuse, otherwise, insert a new row.

这篇关于如何重用auto_increment值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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