How to Number Rows Automatically in SQLite with INTEGER PRIMARY KEY
Most tables need an ID for each row, and nobody wants to pick the numbers by hand. In SQLite, a column declared INTEGER PRIMARY KEY is numbered for you: leave it out of the INSERT, and the new row gets the next number. This guide numbers a list of tasks, with real output from SQLite 3.54.1.
Syntax
CREATE TABLE <table> (
<id column> INTEGER PRIMARY KEY,
<other column> <type>
);
INSERT INTO <table> (<other column>) VALUES (<value>);
| Part | What it means |
|---|---|
<id column> INTEGER PRIMARY KEY | The row's number. The type must be written exactly INTEGER |
INSERT INTO <table> (<other column>) | List every column except the ID, and SQLite fills the ID in |
SQLite's AUTOINCREMENT page: "On an INSERT, if the ROWID or INTEGER PRIMARY KEY column is not explicitly given a value, then it will be filled automatically with an unused integer, usually one more than the largest ROWID currently in use."
Prerequisites
- The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run
sqlite3 --versionto see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.
Step 1: Write the Script
Save this as auto_number.sql:
CREATE TABLE tasks (
task_id INTEGER PRIMARY KEY,
title TEXT NOT NULL
);
INSERT INTO tasks (title) VALUES ('Buy milk');
INSERT INTO tasks (title) VALUES ('Call the bank');
INSERT INTO tasks (title) VALUES ('Water the plants');
SELECT task_id, title FROM tasks;

task_id INTEGER PRIMARY KEYis the column SQLite numbers.- Each
INSERTgives only atitle. None of them mentionstask_id. SELECT task_id, title FROM tasksshows the numbers SQLite chose.
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table todo.db ".read auto_number.sql"
sqlite3 opens the database file todo.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.
+---------+------------------+
| task_id | title |
+---------+------------------+
| 1 | Buy milk |
| 2 | Call the bank |
| 3 | Water the plants |
+---------+------------------+

The three tasks got the numbers 1, 2 and 3 without any of them being written down. The table started empty, so the first row got 1, and each next row got one more than the highest so far.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
INT PRIMARY KEY instead of INTEGER PRIMARY KEY | No numbers: task_id stayed NULL. Only the exact type name INTEGER gives automatic numbers | Write INTEGER in full |
Giving the ID a text value, such as 'first' | datatype mismatch | Leave the ID out, or give it a whole number |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The ID column shows nothing for new rows | The type is INT, BIGINT or another name, not exactly INTEGER | Recreate the table with INTEGER PRIMARY KEY |
Parse error near line 1 of auto_number.sql: table tasks already exists | The script already ran on todo.db | Delete todo.db and run the script again |
FAQ
Can I still choose an ID myself?
Yes, include it in the INSERT. After task_id 10 was given by hand, the next row without an ID got 11.
Can a number be used twice?
Not by two rows at once, but a deleted number can come back. After deleting task 3, the highest, the next new task got 3 again. Adding AUTOINCREMENT, as in INTEGER PRIMARY KEY AUTOINCREMENT, stops that: the same steps gave 4. SQLite's documentation says it "should be avoided if not strictly needed", because it costs extra CPU, memory, disk space and disk I/O.
What's the rowid?
Every ordinary SQLite table has a hidden whole-number rowid for each row. An INTEGER PRIMARY KEY column is another name for it, so SELECT rowid, task_id FROM tasks gave the same number twice.
Conclusion
task_id INTEGER PRIMARY KEY numbered the tasks 1, 2 and 3 on its own, and an INSERT only had to give the title.
Additional Resources
- SQLite Autoincrement — how SQLite picks the next number, with and without
AUTOINCREMENT. - ROWIDs and the INTEGER PRIMARY KEY — why only
INTEGERcounts.
