0

I have a table in MySQL that has 997653 rows, but after I have deleted a few, it is now 994332 rows. However, the last number of the ID column is still 997653. How can I reset it to recount the columns?

I have tried ALTER TABLE foo AUTO_INCREMENT=1; but that did not work.

3
  • 3
    You don't. auto_increment doesn't correspond to number of rows and you are not supposed to reset it. Its job isn't to be sequential but unique. To do so, auto_increment uses a simple algorithm that increments last number by auto_increment_offset - which gives it infinite unique values and works fast in concurrent environment. When something is deleted or an insert fails, the value is forever spent and is never reused. You have no problem unless you tamper with auto_increment. Commented Jul 20, 2017 at 14:22
  • What engine uses the table? InnoDB or MyISAM? Commented Jul 20, 2017 at 14:25
  • 2
    What I find ridiculous is that people, even though they're aware that there's no problem at all and that this issue is caused by OP's OCD and fallacy, they still provide solution for shooting oneself in the foot. Seriously guys, what gives? Are imaginary internet points so valuable that you will skip on actually educating people in order to get upvotes? Pathetic. Commented Jul 20, 2017 at 14:32

2 Answers 2

1

That's the right syntax for reseeding but you should never reseed the auto_increment column value. Yes once you delete rows the auto_increment value will keep going for next value but that doesn't cause any effect to your table or queries made on that table.

if matter, you can even consider performing a soft delete instead doing a hard delete issuing delete from... statement

Sign up to request clarification or add additional context in comments.

Comments

1

If the table engine is InnoDB then it's enough to restart the MySQL service (if this is possible on your setup).

The InnoDB table engine doesn't store the auto-increment value anywhere. When the server starts, it finds the largest value present in the table (it's easy, the AUTO_INCREMENT column is always the table's PK), increments it and this is the new value it uses on the next INSERT.

It is explained in the documentation:

If you specify an AUTO_INCREMENT column for an InnoDB table, the table handle in the InnoDB data dictionary contains a special counter called the auto-increment counter that is used in assigning new values for the column. This counter is stored only in main memory, not on disk.

To initialize an auto-increment counter after a server restart, InnoDB executes the equivalent of the following statement on the first insert into a table containing an AUTO_INCREMENT column.

SELECT MAX(ai_col) FROM table_name FOR UPDATE; 

InnoDB increments the value retrieved by the statement and assigns it to the column and to the auto-increment counter for the table. By default, the value is incremented by 1.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.