views:

86

answers:

5

I was wondering what the simplest way of letting a table set ids automatically is, apart from AUTO_INCREMENT.

I have a table that will accumulate thousands of rows over a long period of time. Some of these rows will be deleted at a later time. How do I get new rows to get the lowest available id, without getting into doing it manually.

Can I for instance reset AUTO_INCREMENT every time someone adds a row?

+4  A: 

Frankly it isn't worth the hassle. I believe its possible, but it's just a unique identifier at the end of the day - doesn't matter if its 1 or 1000.

If it's for other purposes, consider using another field.

IMO you shouldn't mess with the indexes.

Ross
+2  A: 

Not possible, an ID is unique and can be used once..

only possible if you Empty your table.

Jordy
That's wrong. It can only be used once at the same time, but it might be used again if you have deleted an entry.
Kau-Boy
right @Kau-Boy :)
faileN
@Kau no, you are wrong. If one's child would die, and they bore another one, it would be still another one, even if named after first. You misunderstood unique identification concept.
Col. Shrapnel
@Col. Shrapnel: Jordy was saying that you can never use a value again that was once used in a column named "ID" and that is wrong. The data and therefor the new row might be different, but you can use the same value in the column "ID" as long as the previous one was deleted.
Kau-Boy
@Kau oh shit it's sophistry. Yes, literally you *can* shoot someone in the head. But you shouldn't do that, "you can't" in terms of real life.
Col. Shrapnel
@Kau-Boy , even when you delete the row it's impossible to get the unique ID. Once given you can't give a row that ID. Example: I give you my shoes, after i gave them to you.. I give my shoes to Col. Shrapnel, but wait! You have them.. so I give him my empty hands.
Jordy
I'm just talking about the number in the ID field, not dead children or shoeless hands. I know I can set the ID field to whatever I want as long as it's not taken, I just wanted AUTO_INCREMENT to do that for me.
Codemonkey
+1  A: 

Who cares? An integer can have billions of unique values, all of them cost the same amount of memory (4 bytes each). Gambling with the primary key is like russian roulette to your database: In the end your database will die (it gets corrupted).

Frank Heikens
A: 

I guess you could use triggers for this, but this will probably turn into a huge mess after a while. Don't see why you wouldn't want to use auto_increment instead, do you have any particular reason?

Triggers in MySQL 5.0 - http://dev.mysql.com/doc/refman/5.0/en/triggers.html

@Jordy: Your statement is not all true, yes only one row can have that one value at any given point, but if you delete a row you can still reuse the deleted value for another row. You can also reset the counter to any value you want to including the increment without deleting the table.

skogen
A: 

It is possible to reset the auto increment to the next possible ID:

ALTER TABLE table_name AUTO_INCREMENT = 1

You don't even have to set it to the next available key. MySQL will automatically take the next key on the next update. So even setting it to "1" every time is safe.

But if you don't care about the ID why do you even use a ID? And also even large numbers of IDs are not a problem. I got a table with more than 20 million entries and never had a problem with the IDs.

Kau-Boy
Why do people always down vote possible answers on the OP question without explaining them?
Kau-Boy
I voted your reply up and marked it the correct answer. I know this is rarely needed and should me solved by other means, but a developer has to know the bad practices in order to avoid them. Anything else would be dumb.
Codemonkey
The reason I needed this is that this particular table could receive millions of rows, but not necessarily contain more than tens or hundreds of rows at one time. Seemed a little overkill for the ids to be in the millions in that case.But thanks for your reply!
Codemonkey
No problem. Sometimes the other users just don't want to answer a question because they think that the way you yre doing it is wrong. I also tell the OP sometimes how to do it, but you shouldn't be down voted if you find a valid solution. Even programming is not always black or white.
Kau-Boy