✅ gaps in database keys
I don't know if this is the best place to post it as it is more related to databases. However.
I am currently learning Razor Pages, and trying to work with databases for the first time. I went with PostgreSQL. I did all the CRUD operations. And i went with the auto-incrementing property on my key, so the database deals with it. However if i delete elements the key value doesn't reset and there are gaps.
My first question would be. Are these gaps in the key values acceptable or should i deal with them somehow?
My second question is. PostgreSQL offers the ability tu TRUNCATE the data. Which resets the key values and starts from 1 again. Would it be a wise thing to do if i truncate it if the database is empty so the values start back from one?
Basically, to put it in short. Do i do anything about the gaps in the database key values or just leave as is?
2 Replies
Yes, gaps will happen and it's fine
Auto-incremented IDs are sequential. If you delete something with ID 5, the database won't go back and start filling that gap, but will just continue with further numbers
TRUNCATE
deletes all rows. So sure, use it if you want to delete all the data, or otherwise reset the tableto clarify, the only "gap" is in the sequence of IDs
it's not like there is storage being wasted on rows in gaps between IDs
generally the most important thing is just that your IDs are unique, the exact value doesn't matter