1

Let's say I have Sqlite table with a column which its datatype is INTEGER PRIMARY KEY AUTOINCREMENT If I add a row into it I get a row with id of 1, then if I delete that row and add another row its id will be 2 instead of 1

How can I decrement the id whenever I delete a row?

Thank you

jarlh
  • 42,561
  • 8
  • 45
  • 63
  • If you need to decrement the ID each time a row is deleted, probably the `autoincrement` option is not the right solution for your case. BTW read [this answer](https://stackoverflow.com/a/29528936/2910520) to achieve what you need – MatPag Dec 04 '17 at 09:51

1 Answers1

0

You cannot tweak an auto-incremented id manually as you edit the table, because it's meant to work that way. If it could go back and forth, you wouldn't be able to assert that the ID would be unique.

The best options you currently have are:

  1. Use a GUID instead, which solves your uniqueness issue for 99.999% of your cases.
  2. Add a manually-filled order field. You can use this field separately to calculate the number you want to use.
  3. Truncate the table. If you do this, you'll reset the table's ID, but you'll lose the data.
Héctor Álvarez
  • 494
  • 3
  • 18