0

Possible Duplicate:
How to fill in the “holes” in auto-incremenet fields?

We are currently using auto-increment on a table where entries come and go constantly. The problem with this, is that eventually the auto-increment id becomes huge, as that is how auto-increment works.

We would like to have it always add +1 to the last entry.

For example:
We have 4 entries and id 4 is deleted. Next added entry should get id 4, and not 5.

I am not sure if this has been asked before. But after searching I was only able to find solutions on how to get the next auto-increment number, which is not what I am looking for at all.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Xen
  • 442
  • 1
  • 4
  • 13
  • What is the problem with the auto-increment ? – Jscti Oct 30 '12 at 12:52
  • 1
    I strongly suggest against such scheme. If you re-use id's, you can end up with references to wrong rows, instead of broken references. It can be really hard to debug and can lead to security vulnerabilities. – Ilmo Euro Oct 30 '12 at 12:53
  • What's the problem with autoincrement values? It's designed for exactly this, shouldn't matter how "huge" it gets; and "rolling your own" using last entry + 1 is going to give you constant problems – Mark Baker Oct 30 '12 at 12:53
  • Could you explain why you are doing this? The idea of auto-incriment is so that you always have a unique key for your DB table. Typically, these keys do not need to be changed (and regardless of the number, it won't slow your DB down - even if the numbers are 1, 2, 3 and 7463, there are still only 4 entries). – David Gard Oct 30 '12 at 12:54
  • This does actually get asked pretty often, and the answer is always the same - it is highly inadvisable to attempt to fill in the gaps. Many solutions to renumbering on the fly are prone to race conditions. – Michael Berkowski Oct 30 '12 at 12:54

4 Answers4

1

YOU SHOULD NOT DO THAT because auto_increment is designed this way for good reasons (like if you have a backup and you want to restore it when it contains old deleted id that has been rewrote, how do you do ?)

But to answer your question: You have to use

ALTER TABLE `table` AUTO_INCREMENT = MAX(id)+1;

After a delete, you can make a trigger

Jonathan Muller
  • 7,348
  • 2
  • 23
  • 31
0

If you really want to do that (I personally encourage you to use auto-increment), you need to perform a transaction in order to get the last ID and insert the new row setting its ID incrementing it according to the other one.

Carlos
  • 4,949
  • 2
  • 20
  • 37
-1

It's not a good idea to do this, the ID should always stay unique, no matter if the record exists or is deleted.

However if you really want to do it, you can to it with something like

select max(id) + 1 of bla;

But you need the right transaction level for it because if you don't you have a possibility of duplicated ids.

OschtärEi
  • 2,255
  • 3
  • 20
  • 41
-3

You can choose maximum id value and just increment it.

SELECT MAX(`id`)+1 as `new id` FROM `table`;

If you have 5 entries and 3rd gets deleted, this will still get you 6 as next id.

Buksy
  • 11,571
  • 9
  • 62
  • 69