Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Can't set auto-increment value of tables to lower value

I used to be able to set an auto-increment value to lower than the existing one after deleting rows in a table, but now I am unable to do so (I can only set it to higher than the current value). I'm wondering if this is because of upgrading MySQL (5.6.11)/PHPMyAdmin (3.5.8), or whether I should still be able to do this?

like image 554
Nick Avatar asked May 26 '13 18:05

Nick


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 get the last ID from a table if it's set to auto increment?

To get the next auto increment id in MySQL, we can use the function last_insert_id() from MySQL or auto_increment with SELECT. Creating a table, with “id” as auto-increment.

How do you prevent the auto increment being reset when you delete all the rows of a table?

If you delete the row containing the maximum value for an AUTO_INCREMENT column, the value is not reused for a MyISAM or InnoDB table. If you delete all rows in the table with DELETE FROM tbl_name (without a WHERE clause) in autocommit mode, the sequence starts over for all storage engines except InnoDB and MyISAM.

What happens if MySQL auto increment reaches limit?

When the AUTO_INCREMENT column reaches the upper limit of data type then the subsequent effort to generate the sequence number fails. That is why it is advised to use a large enough integer data type for the AUTO_INCREMENT column to hold the maximum sequence value required by us.


1 Answers

You can reset the hidden value by restarting the MySQL server. InnoDB recalculates the auto-increment value after restart.

Update your rows, and restart the mysql deamon. This prevents you from rebuilding your table what can be a hastle with big tables.

like image 182
user3145885 Avatar answered Sep 30 '22 05:09

user3145885