How to reset AUTO_INCREMENT in MySQL?

2018-12-31 02:13发布

How can I reset the AUTO_INCREMENT of a field? I want it to start counting from 1 again.

22条回答
大哥的爱人
2楼-- · 2018-12-31 02:51

You can simply truncate the table to reset the sequence

TRUNCATE TABLE TABLE_NAME
查看更多
其实,你不懂
3楼-- · 2018-12-31 02:52

You can also use the syntax TRUNCATE table like this : TRUNCATE TABLE table_name

BEWARE!! TRUNCATE TABLE your_table will delete everything in your your_table!!

查看更多
笑指拈花
4楼-- · 2018-12-31 02:53

Best way is remove the field with AI and add it again with AI, works for all tables

查看更多
无与为乐者.
5楼-- · 2018-12-31 02:55
ALTER TABLE tablename AUTO_INCREMENT = 1
查看更多
人气声优
6楼-- · 2018-12-31 02:57

I suggest you to go to Query Browser and do the following:

  1. Go to schemata and find the table you want to alter.
  2. Right click and select copy create statement.
  3. Open a result tab and paste the create statement their.
  4. Go to the last line of the create statement and look for the Auto_Increment=N, (Where N is a current number for auto_increment field.)
  5. Replace N with 1.
  6. Press ctrl+enter.

Auto_increment should reset to one once you enter new row int the table.

I don't know what will happen if you try to add a row where an auto_increment field value already exist.

Hope this help!

查看更多
几人难应
7楼-- · 2018-12-31 02:58

As of MySQL 5.6 the approach below works faster due to online DDL (note algorithm=inplace):

alter table tablename auto_increment=1, algorithm=inplace;

查看更多
登录 后发表回答