问题背景: 在使用删除记录之后,自增的id不会应为记录的更改而重新编排,从而导致id的断层现象.
有以下方法:
- 重置清空id (在不需要所有的记录的情况下,该方法
会将表清空) - 删除id字段并且重建(全部重新排序)
- 重新设置排序起始(在最大编号之后继续排序)
一: 重置清空id (在不需要所有的记录的情况下,该方法会将表清空)
命令:
truncate table 表名
https://jingyan.baidu.com/article/22a299b5fecb789e18376a66.html
二:删除id字段并且重建
alter table 表名 drop 字段;
alter table 表名 add 字段 int(3) not null first;
alter table 表名 modify column 字段 int( 3 ) not null auto_increment,add primary key(字段);
select * from 表名 order by 字段 ;
--------------------------------------------------------------------------------------------------
例如:
alter table category drop id;
alter table category add id int(3) not null first;
alter table category modify column id int( 3 ) not null auto_increment,add primary key(id);
select * from category order by id ;
该方法会将所有的记录重新排序
https://blog.youkuaiyun.com/weixin_42321963/article/details/82751622
三:重新设置排序起始
alter table 表名 AUTO_INCREMENT=n
例如:
alter table admin AUTO_INCREMENT=1
该方法会根据已有的记录找出最大的编号,并且在这之后往下排序,不会将中间缺少的序号补上
https://blog.youkuaiyun.com/qq_42690512/article/details/95618558

本文介绍了解决数据库自增ID断层问题的三种方法:重置清空ID、删除并重建ID字段、重新设置排序起始。每种方法都有详细的SQL命令示例,适用于不同场景的需求。
752

被折叠的 条评论
为什么被折叠?



