mysql - 如何在 MySQL 中重置 AUTO_INCRMENT

标签 mysql sql auto-increment

如何重置字段的AUTO_INCRMENT

我希望它再次从 1 开始计数。

最佳答案

您可以通过以下方式重置计数器:

ALTER TABLE tablename AUTO_INCREMENT = 1

InnoDB

对于InnoDB您不能将 auto_increment 值设置为低于或等于当前最高索引。 (引自ViralPatel):

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

咏叹调

在带有 Aria 的表格中存储引擎的auto_increment值可以设置为任何值,甚至低于当前最大值。然而,下一个插入使用下一个可用值(max + 1),忽略设置值。如果设置为更高的值,它将继续使用并从此增加。文档对此并没有特别明确,但这是在 Mariadb 10.11.3 中观察到的

另请参阅

参见 How can I reset an MySQL AutoIncrement using a MAX value from another table? 关于如何动态获取可接受的值。

关于mysql - 如何在 MySQL 中重置 AUTO_INCRMENT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60984570/

相关文章:

mysql - SQL 结构通过随时间的变化来计算队列的大小

php - COALESCE 不同表中的所有字段 JOIN

java - autonicrement berkeley db,或列表

php - 查询结果中的</p>是什么意思?

MySQL 根据另一个表值更新表

php - 搜索其值中包含最后一个字符的表列

java - JPA Tools/EclipseLink - 从实体生成表 - 列顺序

c - void * 算术

sql - Oracle数据库中的自动增量主键

php - 仅比较来自数据库 Timestamp Laravel 的日期