0

Is there a way to have SQLite force rowids to be sequential and start from 0? E.g. if I have this table:

    CREATE TABLE memory_by_cpu (
        cpu INTEGER PRIMARY KEY,
        memory INTEGER NOT NULL
    )

I want to ensure that the cpu column (which will be equal to the rowid) is sequential and starting at 0.

This is unrelated to AUTOINCREMENT.

Timmmm
  • 88,195
  • 71
  • 364
  • 509
  • Does this answer your question? [Set start value for AUTOINCREMENT in SQLite](https://stackoverflow.com/questions/692856/set-start-value-for-autoincrement-in-sqlite) – forpas Sep 29 '20 at 13:44
  • Also: https://stackoverflow.com/questions/35043256/sqlite-starting-rowid-of-0 – forpas Sep 29 '20 at 13:45
  • No, this is unrelated to `AUTOINCREMENT` which does not enforce anything, it just sets a default value on `INSERT` if you don't supply one. – Timmmm Sep 29 '20 at 16:40
  • You want DB to throw an error if an insert is not sequential? – m0skit0 Sep 29 '20 at 16:44
  • 2
    @Timmmm take some time and read this: https://sqlite.org/autoinc.html – forpas Sep 29 '20 at 16:51
  • @m0skit0: Exactly – Timmmm Sep 30 '20 at 08:57
  • @formpas: I have. It is not relevant. – Timmmm Sep 30 '20 at 08:57
  • SQLite does not support that. You would need to check manually before the insertion. It would be interesting to know why you want this behavior. – m0skit0 Sep 30 '20 at 10:11
  • Same reason any constraints exist: because my data should alway fit this pattern, and it simplifies my code if I can have SQLite enforce the condition rather than having to retrieve the data and manually check it myself. – Timmmm Sep 30 '20 at 13:20

0 Answers0