SQL DEFAULT
The DEFAULT constraint gives a column a value to use when an INSERT does not mention it. Without a default, the column would become NULL.
CREATE TABLE t (
col datatype DEFAULT value,
...
);A simple default
In TAD Market, products.stock is INTEGER NOT NULL DEFAULT 0:
INSERT INTO products (name, price) VALUES ('Reusable Bag', 1.5);
SELECT name, price, stock FROM products WHERE name = 'Reusable Bag';1 row affected.
name | price | stock
-------------+-------+------
Reusable Bag | 1.5 | 0
(1 row)
Several defaults
CREATE TABLE newsletter (
email TEXT PRIMARY KEY,
language TEXT NOT NULL DEFAULT 'en',
frequency TEXT NOT NULL DEFAULT 'weekly',
confirmed INTEGER NOT NULL DEFAULT 0
);
INSERT INTO newsletter (email) VALUES ('ava.nguyen@example.com');
INSERT INTO newsletter (email, language) VALUES ('liam.schmidt@example.com', 'de');
INSERT INTO newsletter (email, frequency, confirmed) VALUES ('noah.kim@example.com', 'monthly', 1);
SELECT * FROM newsletter ORDER BY email;1 row affected.
1 row affected.
1 row affected.
email | language | frequency | confirmed
-------------------------+----------+-----------+----------
ava.nguyen@example.com | en | weekly | 0
liam.schmidt@example.com | de | weekly | 0
noah.kim@example.com | en | monthly | 1
(3 rows)
DEFAULT vs explicit NULL
A default is used only when the column is left out. Writing NULL explicitly stores NULL (or fails on a NOT NULL column). The keyword DEFAULT VALUES inserts a row made entirely of defaults:
CREATE TABLE counters (
id INTEGER PRIMARY KEY,
hits INTEGER DEFAULT 0,
label TEXT DEFAULT 'unnamed'
);
INSERT INTO counters (hits, label) VALUES (NULL, NULL);
INSERT INTO counters DEFAULT VALUES;
SELECT * FROM counters ORDER BY id;1 row affected.
1 row affected.
id | hits | label
---+------+--------
1 | NULL | NULL
2 | 0 | unnamed
(2 rows)
Expressions as defaults
A default can be an expression in parentheses. A typical example is the current date — DEFAULT CURRENT_DATE or DEFAULT (date('now')). Because the playground output must be the same on every run, here is a fixed expression instead:
CREATE TABLE price_history (
product_id INTEGER NOT NULL,
price REAL NOT NULL,
valid_from TEXT NOT NULL DEFAULT (date('2025-01-01', '+1 year')),
note TEXT DEFAULT ('auto-' || 'import')
);
INSERT INTO price_history (product_id, price) VALUES (1, 25.9);
SELECT * FROM price_history;1 row affected.
product_id | price | valid_from | note
-----------+-------+------------+------------
1 | 25.9 | 2026-01-01 | auto-import
(1 row)
In real tables you would write:
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP -- '2025-10-16 09:30:00' (UTC)Adding or changing a default
| Database | Syntax |
|---|---|
| SQLite | ALTER TABLE t ADD COLUMN c TEXT DEFAULT 'x' (only for new columns) |
| PostgreSQL | ALTER TABLE t ALTER COLUMN c SET DEFAULT 'x' |
| MySQL | ALTER TABLE t ALTER c SET DEFAULT 'x' |
| SQL Server | ALTER TABLE t ADD CONSTRAINT df_c DEFAULT 'x' FOR c |
Tip:
NOT NULL DEFAULT …is a great pair: inserts stay short, and the column can never be empty.
Test yourself
When is a column's DEFAULT used?
- Always
- When the INSERT leaves the column out
- When the INSERT sets it to NULL
- On every UPDATE
What does INSERT INTO t DEFAULT VALUES; do?
- Inserts nothing
- Inserts a row using every column's default
- Resets all rows to defaults
- Is an error