SQL AUTOINCREMENT
Most tables use a number as primary key, and you don't want to invent that number yourself. Auto-increment columns let the database generate a new, unique id for each inserted row.
Generated ids in SQLite
In SQLite, any column declared INTEGER PRIMARY KEY gets an automatic value when you leave it out:
CREATE TABLE notes (
note_id INTEGER PRIMARY KEY,
text TEXT NOT NULL
);
INSERT INTO notes (text) VALUES ('Call Highland Roasters');
INSERT INTO notes (text) VALUES ('Restock avocados');
INSERT INTO notes (text) VALUES ('Order new labels');
SELECT * FROM notes ORDER BY note_id;1 row affected.
1 row affected.
1 row affected.
note_id | text
--------+-----------------------
1 | Call Highland Roasters
2 | Restock avocados
3 | Order new labels
(3 rows)
The new id is normally the largest existing id plus 1. You can still supply an id explicitly, as long as it is unique.
INTEGER PRIMARY KEY vs AUTOINCREMENT
Adding the keyword AUTOINCREMENT changes one detail: ids of deleted rows are never reused. Without it, deleting the row with the highest id lets SQLite hand out that id again:
CREATE TABLE plain (id INTEGER PRIMARY KEY, v TEXT);
CREATE TABLE strict (id INTEGER PRIMARY KEY AUTOINCREMENT, v TEXT);
INSERT INTO plain (v) VALUES ('a'), ('b'), ('c');
INSERT INTO strict (v) VALUES ('a'), ('b'), ('c');
DELETE FROM plain WHERE id = 3;
DELETE FROM strict WHERE id = 3;
INSERT INTO plain (v) VALUES ('d');
INSERT INTO strict (v) VALUES ('d');
SELECT 'plain' AS tbl, id, v FROM plain
UNION ALL
SELECT 'strict', id, v FROM strict
ORDER BY tbl, id;3 rows affected.
3 rows affected.
1 row affected.
1 row affected.
1 row affected.
1 row affected.
tbl | id | v
-------+----+--
plain | 1 | a
plain | 2 | b
plain | 3 | d
strict | 1 | a
strict | 2 | b
strict | 4 | d
(6 rows)
AUTOINCREMENT remembers the highest id ever used in the internal sqlite_sequence table:
CREATE TABLE tickets (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT);
INSERT INTO tickets (title) VALUES ('x'), ('y');
DELETE FROM tickets;
SELECT name, seq FROM sqlite_sequence;2 rows affected.
2 rows affected.
name | seq
--------+----
tickets | 2
(1 row)
SQLite's documentation recommends plain INTEGER PRIMARY KEY unless you really need ids that are never reused, because AUTOINCREMENT costs a little extra work.
Other databases
| Database | Auto-increment column |
|---|---|
| SQLite | id INTEGER PRIMARY KEY (optionally AUTOINCREMENT) |
| MySQL / MariaDB | id INT AUTO_INCREMENT PRIMARY KEY |
| PostgreSQL | id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY (or older SERIAL) |
| SQL Server | id INT IDENTITY(1,1) PRIMARY KEY |
| Oracle 12c+ | id NUMBER GENERATED BY DEFAULT AS IDENTITY |
| MS Access | id AUTOINCREMENT PRIMARY KEY |
MySQL and SQL Server let you start at a different number and step size, e.g. IDENTITY(1000, 5) or AUTO_INCREMENT = 1000.
Finding the id you just inserted
INSERT INTO employees (first_name, last_name, title, hire_date, manager_id, salary)
VALUES ('Nina', 'Kovac', 'Sales Rep', '2025-10-01', 2, 56000)
RETURNING employee_id;
SELECT last_insert_rowid() AS last_id;employee_id
-----------
11
(1 row)
last_id
-------
11
(1 row)
Note: Generated ids can have gaps — after deletes, failed inserts or rolled-back transactions. Never rely on ids being consecutive, and don't use them to count rows.
Test yourself
In SQLite, which declaration gives a column automatic ids?
- id INT AUTO_INCREMENT
- id INTEGER PRIMARY KEY
- id SERIAL
- id IDENTITY
What does SQLite's AUTOINCREMENT keyword add?
- Automatic ids at all
- Ids of deleted rows are never reused
- Ids start at 0
- Ids are random