0

What I want to do is reset id from my database table.

Example, my table is:

id         data
1          Tony
3          Johnny
5          Julia

I want to reset the id become like this:

id         data
1          Tony
2          Johnny
3          Julia

How can I do it using mysql query in Java? Help me please.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • In MySQL, there is no such thing as resetting primary keys, you have to do it manually. See also http://stackoverflow.com/questions/740358/mysql-reorder-reset-auto-increment-primary-key. – idleherb Sep 27 '14 at 10:23
  • 1
    You cannot generate gap-less ids. Don't do it. Why are you interested in a gap-less list of primary key values? – Tiny Sep 27 '14 at 10:47

1 Answers1

0

It's a common misbelief that the id field should "fill the gaps". The id field is just for attaching a unique id to an entity, nothing more. MySQL's incremental id algorithm is very simple. It stores the last used id value, then adds 1 to it, when you insert a new row.

I suggest you to use Java's java.util.UUID class for generating new id's, because incremental id is vulnerable to hacks.

Usage:

String id = UUID.randomUUID();
Andras Szell
  • 527
  • 2
  • 13