mysql--id主键列乱了之后,重新排序

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
有问题一起讨论,欢迎留言。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值