Skip to main content

How to Number Rows Automatically in SQLite with INTEGER PRIMARY KEY

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
CREATE TABLE <table> (
<id column> INTEGER PRIMARY KEY,
<other column> <type>
);

INSERT INTO <table> (<other column>) VALUES (<value>);
PartWhat it means
<id column> INTEGER PRIMARY KEYThe 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 --version to 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:

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;

The auto_number.sql script in a macOS-style code window: a tasks table with task_id INTEGER PRIMARY KEY and a title column, three inserts that give only a title, and a SELECT of task_id and title.

  • task_id INTEGER PRIMARY KEY is the column SQLite numbers.
  • Each INSERT gives only a title. None of them mentions task_id.
  • SELECT task_id, title FROM tasks shows the numbers SQLite chose.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
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.

Output
+---------+------------------+
| task_id | title |
+---------+------------------+
| 1 | Buy milk |
| 2 | Call the bank |
| 3 | Water the plants |
+---------+------------------+

The output in a terminal: the tasks table with task_id 1 for Buy milk, 2 for Call the bank and 3 for 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​

MistakeWhat happensDo this instead
INT PRIMARY KEY instead of INTEGER PRIMARY KEYNo numbers: task_id stayed NULL. Only the exact type name INTEGER gives automatic numbersWrite INTEGER in full
Giving the ID a text value, such as 'first'datatype mismatchLeave the ID out, or give it a whole number

Troubleshooting​

SymptomLikely causeFix
The ID column shows nothing for new rowsThe type is INT, BIGINT or another name, not exactly INTEGERRecreate the table with INTEGER PRIMARY KEY
Parse error near line 1 of auto_number.sql: table tasks already existsThe script already ran on todo.dbDelete 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​