简体   繁体   English

在MYSQL中更改现有表上的auto_increment值

[英]Change auto_increment value on existing table in MYSQL

Is there a way to change the auto increment value? 有没有办法改变自动增量值? What I need is a value to be lowered because for some reason there is a bug in my database. 我需要降低的值是因为由于某种原因数据库中存在错误。

Two id's should match, but the auto incremented id is always one more. 两个ID应该匹配,但是自动递增的ID总是一个。

Is it possible to lower it by 1 on an existing table? 是否可以在现有表上将其降低1? (MYSQL/phpmyadmin) (MYSQL / phpmyadmin)

Yes, it is possible in MySQL. 是的,在MySQL中是可能的。 In you PhpMyAdmin select your table, go to "Operations" tab and there is "auto increment" option where you can set new auto increment value. 在您的PhpMyAdmin中选择表,转到“操作”选项卡,然后使用“自动增量”选项,您可以在其中设置新的自动增量值。

But remember it can break down your whole database and it's definitely not recommended to mess with auto increments. 但是请记住,它可能会破坏您的整个数据库,并且绝对不建议您使用自动增量功能。

You can reset the counter with: 您可以使用以下方法重置计数器:

For InnoDB you cannot set the auto_increment value lower or equal to the highest current index. 对于InnoDB,您不能将auto_increment值设置为小于或等于当前最大索引。

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. 对于MyISAM,如果该值小于或等于AUTO_INCREMENT列中当前的最大值,则该值将重置为当前的最大值加1。 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. 对于InnoDB,如果该值小于该列中的当前最大值,则不会发生错误,并且当前序列值不会更改。 See How to Reset an MySQL AutoIncrement using a MAX value from another table? 请参见如何使用另一个表中的MAX值重置MySQL自动增量? on how to dynamically get an acceptable value. 如何动态获得可接受的值。

Source :-- stackoverflow 资料来源: -stackoverflow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM