MySQL Add x to current Primary Key value
I have a database that contains a Primary Key, but my boss wants me to renumber it to (current PK value) + 800000...
So, PK 1 would become 800001, PK 2354 would be come 802354 etc...
Is there a simple way to do th开发者_JAVA百科is or should I write a script?
Probably the most straightforward way to do this is to drop the primary key from that table (not the column, just the index), update all values to increase by 800000, and then add a primary key for that column again.
alter table rptapp_batches change column id id int not null;
alter table rptapp_batches drop primary key;
update rptapp_batches set id = id + 800000;
alter table rptapp_batches add primary key (id);
alter table rptapp_batches change column id id int auto_increment;
The first and last statements are needed if the pk is also auto_increment
.
精彩评论