我正在尝试迁移到极光MySQL,但遇到了自动增量的问题。使用Aurora MySQL:
create table test (id int NOT NULL AUTO_INCREMENT,Primary Key(id));
insert into test (id) values(0);
insert into test (id) values(0);
insert into test (id) values(0);
update test set id=100 where id=3;
select * from test;
+-----+
| id |
+-----+
| 1 |
| 2 |
| 100 |
+-----+
insert into test (id) values(0);
select * from test;
+-----+
| id |
+-----+
| 1 |
| 2 |
| 4 |
| 100 |
+-----+
With MySQL or MariaDb the last result is:
+-----+
| id |
+-----+
| 1 |
| 2 |
| 100 |
| 101 |
+-----+ 请注意,Aurora MySQL“填补”了as MySQL保持最大值并使用它的空白。
我可以配置极光MySQL来保持相同的自动增量行为吗?如果是这样的话,是怎么做的?
发布于 2021-02-10 09:25:38
迁移表后,您可以更改自动增量值。
语法有点复杂,因为不能直接在ALTER TABLE ... AUTO_INCREMENT中使用查询或变量,但您可以这样做:
SET @maxid = (SELECT MAX(id) FROM test);
set @sql = concat('ALTER TABLE test AUTO_INCREMENT = ', @maxid + 50);
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;你可以用这个fiddle来玩它
https://stackoverflow.com/questions/66129257
复制相似问题