如何在MySQL中重置AUTO_INCREMENT? [英] How to reset AUTO_INCREMENT in MySQL?

查看:131
本文介绍了如何在MySQL中重置AUTO_INCREMENT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何 重置 字段的AUTO_INCREMENT?
我希望它再次从1开始计数.

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

推荐答案

您可以使用以下方法重置计数器:

You can reset the counter with:

ALTER TABLE tablename AUTO_INCREMENT = 1

对于InnoDB,您不能将auto_increment值设置为小于或等于当前最大索引. (引用自 ViralPatel ):

For InnoDB you cannot set the auto_increment value lower or equal to the highest current index. (quote from ViralPatel):

请注意,您不能将计数器重置为小于或等于的值 到已经使用过的任何东西.对于MyISAM,如果值小于 等于或等于当前AUTO_INCREMENT中的最大值 列,该值将重置为当前最大值加一.为了 InnoDB,如果该值小于当前的最大值 列,则不会发生错误,并且当前序列值不会更改.

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.

请参见 如何使用另一个表中的MAX值重置MySQL自动增量? 如何动态获得可接受的值.

See How to Reset an MySQL AutoIncrement using a MAX value from another table? on how to dynamically get an acceptable value.

这篇关于如何在MySQL中重置AUTO_INCREMENT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆