Reorder / reset auto increment primary key

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

    To reset the IDs of my User table, I use the following SQL query. It's been said above that this will ruin any relationships you may have with any other tables.

    ALTER TABLE `users` DROP `id`;
    ALTER TABLE `users` AUTO_INCREMENT = 1;
    ALTER TABLE `users` ADD `id` int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
    

提交回复
热议问题