Reorder / reset auto increment primary key

后端 未结 15 1707
北荒
北荒 2020-11-22 04:39

I have a MySQL table with an auto increment primary key. I deleted some rows in the middle of the table. Now I have, for example, something like this in the ID column: 12, 1

15条回答
  •  离开以前
    2020-11-22 05:15

    SELECT * from `user` ORDER BY `user_id`; 
    
    SET @count = 0;
    
    UPDATE `user`  SET `user_id` = @count:= @count + 1;
    
    ALTER TABLE `user_id` AUTO_INCREMENT = 1;
    

    if you want to order by

提交回复
热议问题