How to reorder a primary key?

前端 未结 5 1085
小蘑菇
小蘑菇 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条回答
  •  梦毁少年i
    2021-01-01 06:49

    Not sure about one command, but you can do it in four commands:

    CREATE TABLE `temp` SELECT * FROM `orig_tbl`;
    TRUNCATE `orig_tbl`;
    -- counter doesn't reset in some old versions
    ALTER TABLE `orig_tbl` AUTO_INCREMENT = 1; 
    -- now we omit primary key column to let it seal the holes
    INSERT INTO `orig_tbl` (`col1`, `col2`) SELECT `col1`, `col2` FROM `temp`;
    

    Unless you're doing this to make it easier to select records randomly, you really should rethink your approach.

提交回复
热议问题