make an ID in a mysql table auto_increment (after the fact)

前端 未结 7 1479
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 20:09

I acquired a database from another developer. He didn\'t use auto_incrementers on any tables. They all have primary key ID\'s, but he did all the incrementing manually, in

7条回答
  •  时光取名叫无心
    2020-11-30 20:38

    I'm guessing that you don't need to re-increment the existing data so, why can't you just run a simple ALTER TABLE command to change the PK's attributes?

    Something like:

    ALTER TABLE `content` CHANGE `id` `id` SMALLINT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT 
    

    I've tested this code on my own MySQL database and it works but I have not tried it with any meaningful number of records. Once you've altered the row then you need to reset the increment to a number guaranteed not to interfere with any other records.

    ALTER TABLE `content` auto_increment = MAX(`id`) + 1
    

    Again, untested but I believe it will work.

提交回复
热议问题