Sub topic 5
Providing Defaults with `DEFAULT`
This chapter assumes you know basic SELECT, INSERT, UPDATE, and DELETE, and understand tables, rows, columns, and common SQL data types.
A DEFAULT value is the value a database supplies when an INSERT statement does not provide a value for a column. Defaults keep repeated application code out of the database and give new rows predictable initial values.
How `DEFAULT` works
Define a default in a column definition:
CREATE TABLE tasks (
task_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
title VARCHAR(200) NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'pending',
priority INTEGER NOT NULL DEFAULT 3,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);The status, priority, and created_at columns receive defaults when they are omitted:
INSERT INTO tasks (title)
VALUES ('Review deployment plan');
SELECT title, status, priority, created_at
FROM tasks;The database supplies 'pending', 3, and the current timestamp. The identity column also receives a generated value.
A default is applied at insert time. It is not a rule that runs after every update, and it does not automatically fill existing rows when you add a column later.
Omitted values and explicit `NULL`
An omitted column and an explicit NULL are different cases.
| Insert form | Result for a column with `DEFAULT 'pending'` |
|---|---|
| Column omitted | The database uses `'pending'` |
| `status = DEFAULT` | The database uses `'pending'` |
| `status = NULL` | The database stores `NULL`, unless `NOT NULL` rejects it |
For example:
INSERT INTO tasks (title, status)
VALUES ('Publish release notes', DEFAULT);
INSERT INTO tasks (title, status)
VALUES ('Archive old branch', NULL);The first statement uses the default. The second statement fails because status is NOT NULL. DEFAULT does not replace every missing-looking value. It only handles omission or the explicit DEFAULT keyword.
Choosing default expressions
A default can be a literal such as 0, 'pending', or FALSE. It can also be an expression supported by the database, such as CURRENT_TIMESTAMP.
Use defaults for values that have a sensible database-wide starting value:
- A creation timestamp
- A workflow status
- A boolean flag such as
is_active - A numeric count that starts at zero
Avoid defaults that hide missing information. For example, using an empty string for an unknown email makes “unknown” indistinguishable from a real but empty value.
Adding a default later
You can change an existing table with ALTER TABLE:
ALTER TABLE tasks
ALTER COLUMN priority SET DEFAULT 3;
ALTER TABLE tasks
ALTER COLUMN priority DROP DEFAULT;These commands affect future inserts. They do not change existing NULL values. Syntax differs between database systems, so check the documentation for your dialect before using migration scripts.
Exercise
A column is defined as status VARCHAR(20) DEFAULT 'new'. What happens when an insert omits status, uses status = DEFAULT, or uses status = NULL?
Show answer
Omitting status and using DEFAULT both store 'new'. Using NULL stores NULL if the column permits it; otherwise, NOT NULL rejects the row.