Databases & SQL Master Course — 052. Defaults and Generated Identifiers
Lesson 052 Defaults and Generated Identifiers
An insertion can omit values that the schema is designed to supply. Defaults fill omitted columns, while SQLite’s INTEGER PRIMARY KEY can generate an identifier. These mechanisms solve different problems, and neither should be mistaken for business validation or a gapless sequence of records.
Prerequisites and objectives
Complete Lesson 051. You should understand explicit INSERT lists, NULL, primary keys, and the rollback rehearsal pattern. You will compare omitted values with explicit NULL, inspect generated identities, and explain why automatic numbering does not make record counts or chronological assumptions reliable.
Practice safety and transaction preview
Use only a new disposable SQLite database containing the fictional setup below. Every write demonstration starts with BEGIN and ends with ROLLBACK, so its changes are inspected and then undone. Run complete blocks on the same connection, outside any existing transaction. Detailed transaction behavior comes later; this is a controlled rehearsal, not permission to change real data.
CREATE TABLE notes (
note_id INTEGER PRIMARY KEY,
body TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'Draft'
);
INSERT INTO notes (note_id, body, status) VALUES (10,'Existing note','Reviewed');
Step 1 Read the two automatic behaviors
status has a default text value Draft. note_id has the special SQLite declaration INTEGER PRIMARY KEY. The existing row uses identity 10 explicitly. body is required and has no default, so the caller must supply a meaningful body value that satisfies the schema.
These choices communicate different intentions. A default status gives newly created notes an initial workflow state. An identifier distinguishes records. Neither mechanism proves the content is correct, and an automatically assigned identity is not a measure of importance or a count of all records ever created.
Step 2 Omit the defaulted column
Leave status out of the INSERT list and inspect the resulting row.
BEGIN;
INSERT INTO notes (body) VALUES ('New note');
SELECT note_id, body, status FROM notes ORDER BY note_id;
ROLLBACK;
The new note receives status Draft and identity 11 in this fixture. The omitted status uses its schema default. The omitted integer primary key receives an unused generated ID. ROLLBACK removes the new note, restoring the single original row.
Step 3 Distinguish explicit NULL
Supplying NULL is not generally the same as omitting a column. The status constraint rejects an explicit missing status instead of substituting Draft.
BEGIN;
INSERT INTO notes (body, status) VALUES ('Missing status', NULL);
SELECT COUNT(*) FROM notes;
ROLLBACK;
The NOT NULL error is intentional, and the row count remains one. Run the rejected statement separately if your editor stops a multi-statement block at errors, then run the inspection and ROLLBACK. Do not remove the constraint or replace missing content merely to make the demonstration succeed.
The integer primary key has a special exception: in an ordinary SQLite rowid table, supplying NULL for that exact kind of key asks SQLite to generate an ID. That does not mean NULL invokes defaults for every other column.

Step 4 Inspect generated IDs
After an insertion, the generated identifier belongs to that inserted record. In this controlled connection, last_insert_rowid() can report the last successful rowid insertion.
BEGIN;
INSERT INTO notes (body, status) VALUES ('Explicit state', 'Ready');
SELECT last_insert_rowid() AS inserted_id;
SELECT note_id, status FROM notes WHERE note_id = 11;
ROLLBACK;
The connection reports 11, and the row has the explicitly supplied Ready status. Do not infer the inserted ID by adding one to a previously observed maximum in application code; another operation can change the state, and generated-ID behavior has edge cases. Use the engine’s supported result mechanism in the actual application workflow.
Step 5 Understand ordinary ID allocation
For this small ordinary rowid table, SQLite normally chooses one more than the largest current rowid when the key is omitted or NULL. The exact declaration matters: INTEGER PRIMARY KEY has special rowid behavior that should not be generalized to every integer-looking type name or every database engine.
Generated IDs need not remain consecutive, and ordinary allocation can reuse some previously deleted values. A gap does not prove data corruption, while a highest ID does not equal the number of rows. Treat the key as identity, not a trustworthy sequence count or timestamp.
Step 6 Know what AUTOINCREMENT changes
SQLite’s AUTOINCREMENT keyword changes the allocation policy to avoid reusing previously committed rowids from that table’s history. It adds bookkeeping and still does not promise a gapless sequence. Rolled-back allocations are not the same as committed historical identities for that policy.
Use AUTOINCREMENT only when its specific non-reuse property is required, rather than adding it reflexively to every primary key. PostgreSQL identity columns and MySQL AUTO_INCREMENT have their own documented semantics; this lesson’s exact behavior is SQLite-specific.
Step 7 Verify defaults as policy
A default is a chosen initial value, not evidence that someone observed or approved that value. Draft is reasonable for this fictional note workflow because it describes a starting state. A default of zero for an unknown balance or count could misrepresent missing information instead.
Inspect both the schema and the inserted row when debugging defaults. An existing table may have a different definition from the script you remember, and explicit input may override the default. The actual column declaration and INSERT list together determine what should happen.
Practice with worked solutions
Exercise 1
Insert a note with an explicit Ready status and inspect it during the rehearsal.
Worked solution
BEGIN;
INSERT INTO notes (body, status) VALUES ('Ready example', 'Ready');
SELECT note_id, body, status FROM notes WHERE note_id = 11;
ROLLBACK;
The explicit status wins because the column was supplied. A default fills an omitted value; it does not override an intentional accepted value.
Exercise 2
Supply NULL for the integer primary key while omitting status.
Worked solution
BEGIN;
INSERT INTO notes (note_id, body) VALUES (NULL, 'Generated key');
SELECT note_id, status FROM notes WHERE note_id = 11;
ROLLBACK;
SQLite generates identity 11 and supplies Draft. These are two separate mechanisms: special key allocation and an ordinary column default.
Exercise 3
Insert the unused explicit key 5. Explain why the stored row count is not the largest ID.
Worked solution
BEGIN;
INSERT INTO notes (note_id, body) VALUES (5, 'Explicit lower key');
SELECT COUNT(*) AS rows, MAX(note_id) AS largest_id FROM notes;
ROLLBACK;
Two rows exist while the largest ID is 10. Identifier values are labels, so maximum identity cannot substitute for COUNT.
Exercise 4
A colleague asks for gapless invoice numbers and proposes this generated primary key. Explain why that requirement needs a separate design.
Worked solution
Ordinary generated keys can have gaps or reuse behavior, and even AUTOINCREMENT does not guarantee consecutive values. A business numbering requirement needs its own documented rules for cancellation, concurrency, retention, and applicable obligations. Do not promise those properties from a storage identity alone.
Knowledge check
Question 1
When does the Draft default apply in the shown inserts?
- When the inserted row receives a generated ID
- When status is omitted
- Whenever status is NULL
- When the body has no default
Answer and explanation
B. Omitting the column requests its default; explicit NULL follows constraint rules.
Question 2
What happens to explicit NULL status?
- It violates NOT NULL
- It generates an ID
- It becomes Ready
- It becomes Draft
Answer and explanation
A. The required status column does not accept that missing value.
Question 3
Which declaration has the demonstrated special key behavior?
- INT alone
- TEXT PRIMARY KEY
- INTEGER PRIMARY KEY
- Any column called id
Answer and explanation
C. SQLite documents special rowid alias behavior for that declaration in an ordinary rowid table.
Question 4
Does AUTOINCREMENT guarantee gapless numbering?
- Always
- No
- Only after rollback
- Yes, if every insert supplies NULL for the key
Answer and explanation
B. Its non-reuse policy is different from consecutiveness.
Question 5
What reliably counts current rows?
- The default value
- The last inserted ID
- The largest generated ID
- COUNT over the intended population
Answer and explanation
D. Identity values do not measure population size.
Pitfalls and summary
Separate defaults from generated identities, omitted values from explicit NULL, and storage keys from business numbering. Inspect actual inserted values and use supported ID retrieval. Automatic behavior is useful when its precise policy matches the data model, not when its name merely sounds convenient.
SQLite behavior and references
Examples were checked with SQLite 3.53.1 on 10 October 2026. Use a SQLite SQL editor; Python is not a prerequisite. Verify dialect-specific functions, comparison rules, and numeric behavior before moving the examples to another engine.
Primary references
- SQLite CREATE TABLE defaults: Default values and constraints.
- SQLite generated rowids: INTEGER PRIMARY KEY allocation and AUTOINCREMENT limits.
- SQLite last inserted rowid: Connection-scoped insertion identity.
- SQLite transactions: Rollback of the practice changes.