Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset the auto increment number/column in a MySql table

Tags:

sql

mysql

The item_category_id column is auto increment value in the mysql table.

I need to delete all the values & next insertion should start from 1. If i delete all values & try to insert a new row then it starts with some 30's & 40's.

  item_category_id  item_category_name
    1                     qqq
    25                    ccc
    32                    vvv
    29                    bb
    4                     bbb
    31                    hhh
    34                    mmm
    33                    rrr
like image 994
Parthi04 Avatar asked May 22 '12 06:05

Parthi04


People also ask

How do I change auto increment?

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 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.

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

You use TRANCATE table to empty the table. TRUNCATE not only deletes the rows but resets the auto increment value by design. Use DELETE FROM table instead.

What will be the auto increment ID of delete and truncate?

Using TRUNCATE TABLE Statement. The TRUNCATE TABLE statement in MySQL completely deletes the table's data without removing a table's structure and always resets the auto-increment column value to zero.


1 Answers

ALTER TABLE TABLENAME AUTO_INCREMENT = 1

Instead of using Delete if you use Truncate that will remove data and will also reset autoincrement.

TRUNCATE TABLE TABLENAME
like image 191
mprabhat Avatar answered Oct 07 '22 17:10

mprabhat