在对数据库进行操作的时候,数据库的表里的id是自增的,当数据被删除或者添加或者插入失败时,id会一直增上去,变得很乱,不会按照顺序,下面是两种解决办法:
-
alter table tablename drop column id; alter table tablename add id mediumint(8) not null primary key auto_increment first;
这个时候就好了,id就会自动续上了!!!
2.
+----+
| 1 |
| 2 |
| 3 |
| 7 |
+----+
看到新插入的记录出现错误时,要继续之前的id序列,需要在删除数据后,重置下自动增长的位置
delete from tt where id=7;
alter table tt auto_increment=4
insert into tt values(null);
select * from tt;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
+----+
重新设置后,自增主键就会接上了,这样可以避免出现id断层
以上两种方法,个人感觉这个方法1较好,不需要考虑id断层的位置!!!
本文章结合了两位博主的文章(感谢)。
原文:https://blog.youkuaiyun.com/aoerqileng/article/details/51329585?utm_source=copy
原文:https://blog.youkuaiyun.com/qq_39569728/article/details/79687684?utm_source=copy