How to handle fragmentation of auto_increment ID column in MySQL

Renumbering will cause confusion. Existing reports will refer to record 99, and yet if the system renumbers it may renumber that record to 98, now all reports (and populated UIs) are wrong. Once you allocate a unique ID it’s got to stay fixed.

Using ID fields for anything other than simple unique numbering is going to be problematic. Having a requirement for “no gaps” is simply inconsistent with the requirement to be able to delete. Perhaps you could mark records as deleted rather than delete them. Then there are truly no gaps. Say you are producing numbered invoices: you would have a zero value cancelled invoice with that number rather than delete it.

Leave a Comment