primary key - MySQL update an AUTO_INCREMENT pk to preserve row ordering -
i have table 3 columns: id, foreign_id, , tag. queries on table ordered first foreign_id, tag, want deprecate tag column in favor of more reliable , auto-generated id. in doing so, need preserve ordering data stored in tag column without keeping tag around. ordering makes sense within scope of foreign_id column.
to solve problem, we've decided update ids within scope of each foreign_id such order of ids preserves tag order information.
how 1 update auto_increment primary key column such gets assigned next value in counter without changing rest of row?
alternatively, how 1 copy entire row (minus pk) new row , delete old row?
select max(id) @maxid the_table; update the_table set id = id + @maxid; set @i := 0; update the_table set id = (@i := @i + 1) order foreign_id, tag; i not 100% positive work; don't kind of things update statements. alternatively, replace last update with:
insert the_table(id, foreign_id, tag) select (@i := @i + 1) `new_id`, foreign_id, tag the_table order foreign_id, tag ; delete the_table id >= @maxid ; in either case, assumes current id values >= 0.
Comments
Post a Comment