简体   繁体   中英

AWS Aurora MySQL

I'm trying to migrate to Aurora MySQL and having problems with auto increment. With 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 |
+-----+ 

Notice that Aurora MySQL "fills" the gap where as MySQL maintains a max and uses that.

Can I configure Aurora MySQL to maintain the same auto increment behavior? If so, how?

After migrating the table you can change the auto increment value.

The syntax is a bit convoluted because it is not possible to directly use a query or a variable with ALTER TABLE... AUTO_INCREMENT but this is how you would proceed:

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;

You can play with it with this fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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