mysql increment primary key ID to reorder

Scenario:
     1 . Increment primary key ID number of the database table after adding and deleting a period of chaos
     2 Dui not increment the table's primary key ID, add and sort increment id.

1. Delete the original primary key

ALTER  TABLE  `table_name` DROP `id`;

2. Add a new primary key field

ALTER  TABLE  `table_name` ADD `id` INT(11) NOT NULL  FIRST;

3. Set a new primary key is generated from an automatic sorting

ALTER  TABLE  `table_name` MODIFY COLUMN  `id` INT(11) NOT NULL  AUTO_INCREMENT,ADD PRIMARY  KEY(id);

 

Guess you like

Origin www.cnblogs.com/chenjw-note/p/12095040.html