SQL defaults and identity columns
Overview
A DEFAULT supplies a value when an INSERT omits the column. An identity column generates identifiers automatically; PostgreSQL uses GENERATED ALWAYS or BY DEFAULT AS IDENTITY. SQLite INTEGER PRIMARY KEY aliases the rowid in ordinary rowid tables, which lets the engine assign an integer when omitted.
First example
CREATE TABLE tasks (id INTEGER PRIMARY KEY, status TEXT DEFAULT 'new');INSERT INTO tasks DEFAULT VALUES;INSERT INTO tasks(status) VALUES (NULL);SELECT id, status FROM tasks ORDER BY id;Observed SQLite result (rows in JSON):
[ [ 1, "new" ], [ 2, null ]]How it works
An explicit NULL for an ordinary nullable column is different from omitting it; the default is not substituted. Identity is not itself a uniqueness constraint in PostgreSQL, so add PRIMARY KEY or UNIQUE where required. Automatically allocated identifiers are not gapless counters and are not business timestamps. PostgreSQL uses sequences for identity; SQLite AUTOINCREMENT is a distinct optional rowid allocation policy, not a requirement for automatic IDs.
Practical example
CREATE TABLE tickets (id INTEGER PRIMARY KEY, title TEXT NOT NULL);INSERT INTO tickets(id,title) VALUES (10,'First');INSERT INTO tickets(title) VALUES ('Second');SELECT id,title FROM tickets ORDER BY id;Observed SQLite result (rows in JSON):
[ [ 10, "First" ], [ 11, "Second" ]]Practice and mistakes
Practice: add a third task with an explicitly chosen status. Predict its generated ID in a fresh copy of the exact example, but do not assume future allocations are consecutive. Compare omitted status with explicit NULL and explain the difference.
PostgreSQL 17 example — NOT RUN
CREATE TABLE tickets (id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, title text NOT NULL);INSERT INTO tickets(title) VALUES ('First');SELECT id,title FROM tickets;Expected result, not executed: [[1, "First"]]