1.问题详述
当我们使用自增变量的时候,删除其中一条数据,其序列不会减小,而是会继续增长,不符合我们的要求,为此提出解决办法。
2.方法
核心思想是删除序列再重新排序,方法如下
alter table category drop cid;
alter table category add cid int(3) not null first;
alter table category modify column cid int( 3 ) not null auto_increment,add primary key(cid);
select * from category order by cid ;
其中
alter table category drop cid;
这一句是移除id;
alter table category add cid int(3) not null first;
这一句是增加id,不过序列全是零。
alter table category modify column cid int( 3 ) not null auto_increment,add primary key(cid);
这一句是id重新排序
通过这种方法就能解决,不过缺点是,如果数据库很大,那么就会比较耗费时间
3.注意
alter table category modify column cid int( 3 ) not null auto_increment,add primary key(cid);
这一句一定要写在一起奥,要不然会报错
4.参考
https://blog.csdn.net/weixin_42321963/article/details/82751622#comments
有问题一起讨论,欢迎留言。