0

When creating a MySQL database, how can I modify the auto_increment option so that the number can start from the last one?

For example, I now have a table which is created from this query.

CREATE TABLE tablename (uid int(11) primary key auto_increment, ... );

If I add data into this table consecutively, each data will have its own uid starting from 1. If I delete the last data with the uid value of 3 and add another new data, the new one will have the uid value of 4, not 3.

I want to know how to make that new data have 3 for the uid.

MarshallLee
  • 1,290
  • 4
  • 23
  • 42
  • @JayBlanchard I already checked out those questions, but they were about resetting the auto_increment at 1. – MarshallLee Jul 08 '15 at 13:32
  • @JayBlanchard What I want to know is how to make those auto_increment values start from the last-added data's value. – MarshallLee Jul 08 '15 at 13:33
  • The answer is still the same, you have to ALTER the table. By careful that you do not use the id's to associate this data with other data, modification of one could seriously fubar the other. – Jay Blanchard Jul 08 '15 at 13:34

1 Answers1

1

After deleting the record you have to fire the following query on database

ALTER TABLE tablename AUTO_INCREMENT = value;

Where value is the 'id' of the deleted row. When next time you add the record it will take the deleted rows id as primary key

Santosh Jagtap
  • 995
  • 8
  • 17