简体   繁体   中英

MySQL: How to update auto-increment for all existing entries?

I have an existing table that uses an auto-incremented id as its primary key.

There are entries in the table with the id starting at 1:

id    field1
==    ======
1     foo1
2     foo2
3     foo3

Is there a way to update the id for all existing entries so the auto_increment starts at another number?:

id    field1
====    ======
1000     foo1
1001     foo2
1002     foo3

(The order does not necessarily have to be kept if that is not possible)

You can use update to change the values and alter table to update the auto increment to a new value:

alter table t set auto_increment = 1003;  -- the next value

update t
    set id = id + 999;

In Sql Server it is work, MySql I'm not sure

DECLARE @NewRowId int = 998;

DBCC CHECKIDENT('MyTableName', RESEED, @NewRowId)

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