MYSQL在不删除数据的情况下,重置主键自增id
方法一:
SET??@num?:=?0;
UPDATE?table_name?SET?id?=?@num?:=?(@num+1);
ALTER?TABLE?table_name?AUTO_INCREMENT?=1;
方法二:
背景(mysql 数据在进行多次删除新增之后id变得很大,但是并没有那么多条数据)
写一个存储过程更新mysql的主键id
话不多说上代码
1.mysql?命令模式下键入?
????delimiter?$$
2.
????create?procedure?update_data_id()
?????begin
????????declare?ind?int;
????????set?ind?=?1;
????????while?ind?<=?现有表数据总量+1
????????do
?????????????UPDATE??表名?set?id?=?ind?where?id?>?ind?LIMIT?1?;
?????????????set?ind?=?ind?+?1;
?????????end??while;
????end?$$
3.delimiter?;
表的总数量暂时没有做到count去取,暂时需要自己去count添加进去。
完毕!!!
alter?table?表名?drop?id;
alter?table?表名?add?id?int?primary?key?not?null?auto_increment?first;