mysql添加删除自增长,mysql删除,自动递增

I have a table in MySQL using InnoDB and a column is there with the name "id".

So my problem is that whenever I delete the last row from the table and then insert a new value, the new value gets inserted after the deleted id.

I mean suppose my id is 32, and I want to delete it and then if I insert a new row after delete, then the column id auto-increments to 33. So the serial format is broken ie,id =30,31,33 and no 32.

So please help me out to assign the id 32 instead of 33 when ever I insert after deleting the last column.

解决方案

Short answer: No.

Why?

It's unnecessary work. It doesn't matter, if there are gaps in the serial number.

If you don't want that, don't use auto_increment.

Don't worry, you won't run out of numbers if your column is of type int or even bigint, I promise.

There are reasons why MySQL doesn't automatically decrease the autoincrement value when you delete a row. Those reasons are

danger of broken data integrity (imagine multiple users perform deletes or inserts...doubled entries may occur or worse)

errors may occur when you use master slave replication or transactions

and so on ...

I highly recommend you don't waste time on this! It's really, really error prone.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值