When should a SQLite table use an INTEGER PRIMARY KEY?
Quick answer
Use INTEGER PRIMARY KEY in a rowid table when you want its single-column key to be an alias for SQLite’s signed 64-bit rowid. The exact type name matters: INT PRIMARY KEY is an ordinary integer-affinity column with a unique index, not that alias. This distinction does not apply to WITHOUT ROWID tables, and INTEGER PRIMARY KEY DESC has a documented exception. [S1]
The question, the details, the next step.
A practical checklist
Decide whether rowid-alias behavior is intended.
Use the exact type name INTEGER for that behavior.
Confirm the table is not WITHOUT ROWID.
Review the declaration for the INTEGER PRIMARY KEY DESC exception.
Do not treat INT or BIGINT as equivalent spellings.
A few more useful answers
Does inserting NULL into an INTEGER PRIMARY KEY create a rowid?
In a rowid table, SQLite chooses a rowid when NULL is inserted into an INTEGER PRIMARY KEY. [S1] This rule does not make INT PRIMARY KEY an alias.
Does INTEGER PRIMARY KEY work in a WITHOUT ROWID table?
A WITHOUT ROWID table has no rowid, so the rowid-alias rule does not apply. [S1]
Is INT PRIMARY KEY still a primary key?
It is a primary-key declaration, but it is an ordinary integer-affinity column with a unique index rather than a rowid alias. [S1]
Sources
References used for this guide.
- [S1] SQLite CREATE TABLE: INTEGER PRIMARY KEY and rowid tables sqlite.org · Checked
