How to reorder a primary key?

前端 未结 5 1109
小蘑菇
小蘑菇 2021-01-01 06:21

I have a table of 5700 records. The primary key is an integer. Now I noticed that some values are missing. Like this:

100 data
101 data 
102 data
104 data
         


        
5条回答
  •  感动是毒
    2021-01-01 06:51

    Another way, without truncating whole table:

    -- Make Backup of original table's content
    CREATE TABLE `orig_tbl_backup` SELECT * FROM `orig_tbl`;
    -- Drop needed column. 
    ALTER TABLE `orig_tbl` DROP `id`;
    -- Re-create it
    ALTER TABLE `orig_tbl` AUTO_INCREMENT = 1;
    ALTER TABLE `orig_tbl` ADD `id` int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
    

提交回复
热议问题