Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the current count of an Auto Increment value in MySQL?

Tags:

mysql

Currently every time I add an entry to my database, the auto increment value increments by 1, as it should. However, it is only at a count of 47. So, if I add a new entry, it will be 48, and then another it will be 49 etc.

I want to change what the current Auto Increment counter is at. I.e. I want to change it from 47 to say, 10000, so that the next value entered, will be 10001. How do I do that?

like image 547
coderama Avatar asked Sep 25 '09 10:09

coderama


People also ask

Can we change auto increment value in MySQL?

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

How do you update auto increment?

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.

How do I change the next Autoindex in MySQL?

ALTER TABLE tbl AUTO_INCREMENT = 66441; If you don't have records ids greater than 66441 then it should work.

How do you adjust the auto increment in SQL After deleting some records from the table?

Reset the auto increment fieldALTER TABLE `table` AUTO_INCREMENT = number; Replacing 'number' with the result of the previous command plus one and replacing table with the table name. If you deleted all the rows in the table, then you could run the alter table command and reset it to 0.


2 Answers

You can use ALTER TABLE to set the value of an AUTO_INCREMENT column ; quoting that page :

To change the value of the AUTO_INCREMENT counter to be used for new rows, do this:

ALTER TABLE t2 AUTO_INCREMENT = value; 

There is also a note saying that :

You cannot reset the counter to a value less than or equal to any that have already been used.
For MyISAM, if the value is less than or equal to the maximum value currently in the AUTO_INCREMENT column, the value is reset to the current maximum plus one.
For InnoDB, if the value is less than the current maximum value in the column, no error occurs and the current sequence value is not changed.

Hope this helps !

like image 188
Pascal MARTIN Avatar answered Sep 30 '22 20:09

Pascal MARTIN


See manual for ALTER TABLE - this should do it:

ALTER TABLE [tablename] AUTO_INCREMENT = [number] 
like image 26
Paul Dixon Avatar answered Sep 30 '22 20:09

Paul Dixon