Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the Auto Increment counter in MySQL?

I have an ID field that is my primary key and is just an int field.

I have less than 300 rows but now every time someone signs up that ID auto inc is inputted really high like 11800089, 11800090, etc.... Is there a way to get that to come back down so it can follow the order (310,311,312).

Thanks!

like image 557
Chris Olson Avatar asked Nov 17 '11 03:11

Chris Olson


People also ask

How do I change the next Autoindex in MySQL?

only way is to pull all the info out (except the id) into a comma seperated values file (or any type of file really), delete the table, rebuild the table and re-insert the data. there is a reason why mysql never uses the lowest available number in auto_increment fields.

How do I reset auto increment counter?

In MySQL, the syntax to reset the AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = value; table_name. The name of the table whose AUTO_INCREMENT column you wish to reset.

Can you set auto increment?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.


1 Answers

ALTER TABLE table_name AUTO_INCREMENT=310; 

Beware though, you don't want to repeat an ID. If the numbers are that high, they got that way somehow. Be very sure you don't have associated data with the lower ID numbers.

https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html

like image 172
Brad Avatar answered Oct 05 '22 12:10

Brad