简体   繁体   English

无法更改MySQL AUTO_INCREMENT

[英]Can't change MySQL AUTO_INCREMENT

I am working on updating a table from using a software generated ID to a MySQL auto_increment ID. 我正在将表从使用软件生成的ID更新为MySQL auto_increment ID的工作。

The ID field is int(10) and database type is InnoDB ID字段为int(10),数据库类型为InnoDB

After updating the IDs to be sequential, starting at 1, the new highest ID is 122. Previously, the highest ID was 62029832 将ID更新为从1开始的顺序ID后,新的最高ID为122。以前,最高ID为62029832

Now I am trying to update the auto_increment value so that the next insert is 123. 现在,我尝试更新auto_increment值,以便下一个插入为123。

The current auto_increment value is 62029833. 当前的auto_increment值为62029833。

So far I have tried: 到目前为止,我已经尝试过:

ALTER TABLE tableName AUTO_INCREMENT = 123; ALTER TABLE tableName AUTO_INCREMENT = 123; --- No luck. - - 没运气。 Doesn't error, just doesn't stick. 没有错误,只是不粘。

INSERT INTO tableName (ID) VALUES (123); INSERT INTO tableName(ID)VALUES(123); DELETE FROM tableName WHERE ID = 123; 从tableName删除,其中ID = 123; --- Still no luck ---仍然没有运气

I would like to avoid truncating the table if there is another method. 如果有其他方法,我想避免将表截断。

From what I've read, InnoDB should allow the change to 123 since the highest value is currently 122, but it's acting as though there is a higher value. 根据我的阅读,InnoDB应该允许将值更改为123,因为当前的最大值是122,但是它的作用好像是有一个更高的值。

Just to test, I've also tried changing the auto_increment to 1000, 2000, 122, etc. Nothing sticks. 为了测试,我还尝试将auto_increment更改为1000、2000、122等。

Any ideas? 有任何想法吗?

After working on it some more, I found a dumb, non-intuitive solution. 经过更多的工作之后,我发现了一个愚蠢的,非直觉的解决方案。

First, remove AUTO_INCREMENT from your ID column. 首先,从您的ID列中删除AUTO_INCREMENT I had foreign key checks on so I had to run: 我检查了外键,因此必须运行:

SET FOREIGN_KEY_CHECKS = 0;
ALTER TABLE `warehouse`.`addresses`
    CHANGE COLUMN `aID` `aID` INT(10) UNSIGNED NOT NULL;
SET FOREIGN_KEY_CHECKS = 1;

Next, update the AUTO_INCREMENT value: 接下来,更新AUTO_INCREMENT值:

ALTER TABLE 'warehouse'.'addresses' AUTO_INCREMENT = 123;

Finally, re-add AUTO_INCREMENT : 最后,重新添加AUTO_INCREMENT

SET FOREIGN_KEY_CHECKS = 0;
ALTER TABLE `warehouse`.`addresses`
    CHANGE COLUMN `aID` `aID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;
SET FOREIGN_KEY_CHECKS = 1;

Hope this helps some poor soul! 希望这可以帮助一些可怜的灵魂!

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

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