views:

693

answers:

7

I have a MySQL table with a primary key field that has AUTO_INCREMENT on. After reading other posts on here I've noticed people with the same problem and with varied answers. Some recommend not using this feature, others state it can't be 'fixed'.

I have:

course table - with 2 fields - courseID, courseName

Example: Number of records in the table: 18
If I delete records 16, 17 and 18 - I would expect the next record entered to have the courseID of 16, however it will be 19 because the last entered courseID was 18.

My SQL knowledge isn't amazing but is there anyway to refresh or update this count with a query (or a setting in the phpmyadmin interface)?

This table will relate to others in a database.

Any help is appreciated...

A: 

you can select the ids like so:

set @rank = 0;
select id, @rank:=@rank+1 from tbl order by id

the result is a list of ids, and their positions in the sequence.

you can also reset the ids like so:

set @rank = 0;
update tbl a join (select id, @rank:=@rank+1 as rank from tbl order by id) b
  on a.id = b.id set a.id = b.rank;

you could also just print out the first unused id like so:

select min(id) as next_id from ((select a.id from (select 1 as id) a
  left join tbl b on a.id = b.id where b.id is null) union
  (select min(a.id) + 1 as id from tbl a left join tbl b on a.id+1 = b.id
  where b.id is null)) c;

after each insert, you can reset the auto_increment:

alter table tbl auto_increment = 16

or explicitly set the id value when doing the insert:

insert into tbl values (16, 'something');

typically this isn't necessary, you have count(*) and the ability to create a ranking number in your result sets. a typical ranking might be:

set @rank = 0;
select a.name, a.amount, b.rank from cust a,
  (select amount, @rank:=@rank+1 as rank from cust order by amount desc) b
  where a.amount = b.amount

customers ranked by amount spent.

+1  A: 

ALTER TABLE foo AUTO_INCREMENT=1

If you've deleted the most recent entries, that should set it to use the next lowest available one. As in, as long as there's no 19 already, deleting 16-18 will reset the autoincrement to use 16.

EDIT: I missed the bit about phpmyadmin. You can set it there, too. Go to the table screen, and click the operations tab. There's an AUTOINCREMENT field there that you can set to whatever you need manually.

monksp
The OP CAN do this, but he shouldn't. You should really reconsider this advice given what the OP is trying to do.
Mike Sherov
It's not my place to tell him how to lay out his database, or how to do his business logic. He also mentioned in his post that he's read other posts/pages stating that it's a bad idea, so he knows that it's not a recommended practice, but is going ahead with it anyway.
monksp
+5  A: 

Primary autoincrement keys in database are used to uniquely identify a given row and shouldn't be given any business meaning. So leave the primary key as is and add another column called for example courseOrder. Then when you delete a record from the database you may want to send an additional UPDATE statement in order to decrement the courseOrder column of all rows that have courseOrder greater than the one you are currently deleting.

As a side note you should never modify the value of a primary key in a relational database because there could be other tables that reference it as a foreign key and modifying it might violate referential constraints.

Darin Dimitrov
he's looking to maintain a count, not an ordering. The UPDATE seems unnecessary.
Mike Sherov
Well if he is looking to maintain a count then there's no need to add additional columns. The simple `count` aggregate function will do the job.
Darin Dimitrov
right, that's what I was trying to get at.
Mike Sherov
Okay, thanks. A lot of answers/comments in such short time! :O I'm trying to take them all in. I will look into the count function :)
HelloWorld
A: 

You can use your mysql client software/script to specify where the primary key should start from after deleting the required records.

Sarfraz
+1  A: 

You shouldn't be relying on the AUTO_INCREMENT id to tell you how many records you have in the table. You should be using SELECT COUNT(*) FROM course. ID's are there to uniquely identifiy the course and can be used as references in other tables, so you shouldn't repeat ids and shouldn't be seeking to reset the auto increment field.

Mike Sherov
+4  A: 

What you're trying to do sounds dangerous, as that's not the intended use of AUTO_INCREMENT.

If you really want to find the lowest unused key value, don't use AUTO_INCREMENT at all, and manage your keys manually. However, this is NOT a recommended practice.

Take a step back and ask why you need to recycle key values? Do unsigned INT (or BIGINT) not provide a large enough key space? Are you really going to have more than 18,446,744,073,709,551,615 unique records over the course of your application's lifetime?

Dolph
You're correct. It's best to ignore it.Considering this is only for an assignment, I won't be going into high numbers and the lifetime is very short.
HelloWorld
agree highly dangerous for a number of reasons. One, in a multi user system you may have many, hundreds, thousands of updates per second and trying to rewrite the auto inc could either slow the system down or compromise it. Two another developer would not know you were =doing this perhaps and link records through the id therefor corrupting the system. etc.
PurplePilot
A: 

Given all the advice, I have decided to ignore this 'problem'. I will simply delete and add records whilst letting the auto increment do it's job. I guess it doesn't really matter what the number is since it's only being used as a unique identifier and doesn't have a (as mentioned above) business meaning.

For those who I may have confused with my original post: I do not wish to use this field to know how many records I have. I just wanted the database to look neat and have a bit more consistency I guess.

Many thanks to all that contributed! Everyone's input helped. This page makes a lot more sense than the others I visited with the same question asked.

HelloWorld