如何在MySQL中重置AUTO_INCREMENT?

本文翻译自:How to reset AUTO_INCREMENT in MySQL?

How can I reset the AUTO_INCREMENT of a field? 如何重置字段的AUTO_INCREMENT I want it to start counting from 1 again. 我希望它再次从1开始计数。


#1楼

参考:https://stackoom.com/question/bRJC/如何在MySQL中重置AUTO-INCREMENT


#2楼

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. 打开结果选项卡,然后将create语句粘贴到它们。
  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.) 转到create语句的最后一行,并查找Auto_Increment = N(其中N是auto_increment字段的当前数字。)
  5. Replace N with 1. 将N替换为1。
  6. Press ctrl + enter . Ctrl + Enter

Auto_increment should reset to one once you enter new row int the table. 在表格中输入新行后,Auto_increment应该重置为1。

I don't know what will happen if you try to add a row where an auto_increment field value already exist. 我不知道如果尝试在已存在auto_increment字段值的行中添加行,该怎么办。

Hope this help! 希望对您有所帮助!


#3楼

There are good options given in How To Reset MySQL Autoincrement Column 如何重置MySQL自动增量列中提供了不错的选择

Note that ALTER TABLE tablename AUTO_INCREMENT = value; 注意, ALTER TABLE tablename AUTO_INCREMENT = value; does not work for InnoDB 不适用于InnoDB的工作


#4楼

The highest rated answers to this question all recommend "ALTER yourtable AUTO_INCREMENT= value". 这个问题的最高评分答案都建议“ ALTER yourtable AUTO_INCREMENT = value”。 However, this only works when value in the alter is greater than the current max value of the autoincrement column. 然而,当这仅适用value在alter比自动增量列的电流最大值。 According to the MySQL 8 documentation : 根据MySQL 8文档

You cannot reset the counter to a value less than or equal to the value that is currently in use. 您不能将计数器重置为小于或等于当前使用的值。 For both InnoDB and 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 AUTO_INCREMENT column value plus one. 对于InnoDB和MyISAM,如果该值小于或等于AUTO_INCREMENT列中当前的最大值,则该值将重置为当前的最大AUTO_INCREMENT列值加1。

In essence, you can only alter AUTO_INCREMENT to increase the value of the autoincrement column, not reset it to 1, as the OP asks in the second part of the question. 从本质上讲,您只能更改AUTO_INCREMENT以增加autoincrement列的值,而不能将其重置为1,这是OP在问题的第二部分中提出的。 For options that actually allow you set the AUTO_INCREMENT downward from its current max, take a look at Reorder / reset auto increment primary key . 对于实际上允许您将AUTO_INCREMENT从当前最大值向下设置的选项,请查看重新排序/重置自动递增主键


#5楼

ALTER TABLE news_feed DROP id

ALTER TABLE news_feed ADD  id BIGINT( 200 ) NOT NULL AUTO_INCREMENT FIRST ,ADD PRIMARY KEY (id)

I used this in some of my scripts , the id field is droped and then added back with previous settings , all the existent fields within the database table are filled in with new auto increment values , this should also work with InnoDB . 我在一些脚本中使用了此功能,先删除id字段,然后再使用以前的设置将其添加回去,数据库表中所有现有的字段都填充有新的自动增量值,这也应与InnoDB一起使用。

Note that all the fields within the table will be recounted and will have other ids !!!. 请注意,表中的所有字段将被重新计数,并将具有其他ID !!!。


#6楼

在此处输入图片说明There is a very easy way with phpmyadmin under the "operations" tab, you can set, in the table options, autoincrement to the number you want. 使用phpmyadmin的“操作”标签下有一种非常简单的方法,您可以在表选项中将自动增量设置为所需的数字。

发布了0 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/p15097962069/article/details/105267807