Reorder / reset auto increment primary key

后端 未结 15 1405
北荒
北荒 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:27

    I had the same doubts, but could not make any changes on the table, I decided doing the following having seen my ID did not exceed the maximum number setted in the variable @count:

    SET @count = 40000000;
    UPDATE `users` SET `users`.`id` = @count:= @count + 1;
    
    SET @count = 0;
    UPDATE `users` SET `users`.`id` = @count:= @count + 1;
    
    ALTER TABLE `users` AUTO_INCREMENT = 1;
    

    The solution takes, but it's safe and it was necessary because my table owned foreign keys with data in another table.

    0 讨论(0)
  • 2020-11-22 05:28

    in phpmyadmin

    note: this will work if you delete last rows not middle rows.

    goto your table-> click on operations menu-> goto table options->change AUTO_INCREMENT to that no from where you want to start.

    your table autoincrement start from that no.

    try it.

    0 讨论(0)
  • 2020-11-22 05:32

    Or, from PhpMyAdmin, remove "AutoIncrement" flag, save, set it again and save.this resets it.

    0 讨论(0)
提交回复
热议问题