Concept · Designing databases

DEFAULT

DEFAULT sets the value a column receives when an INSERT does not supply one. It does not automatically replace an explicit NULL.

DEFAULT example

Create the table
CREATE TABLE orders_demo (
  id INTEGER PRIMARY KEY,
  status VARCHAR(20) NOT NULL DEFAULT 'pending'
);

The value is part of the definition of status. The NOT NULL constraint states a different rule: the column cannot be left without a value.

SQL
INSERT INTO orders_demo (id) VALUES (101);
INSERT INTO orders_demo (id, status) VALUES (102, 'paid');

SELECT id, status
FROM orders_demo
ORDER BY id;

The first insert leaves out status, so it gets 'pending'. The second supplies 'paid' and does not trigger the default.

What to look for: When order 101 is inserted without specifying status, the result is «pending». Specifying «paid» for order 102 keeps that explicit value. The default normally applies when you leave the column out, not when you write NULL.

Check the result with small data

We insert only the identifier, without specifying the status column.

Self-contained test · SQLite
CREATE TABLE demo_status (id INTEGER, status TEXT DEFAULT 'pending');
INSERT INTO demo_status (id) VALUES (42);
SELECT id, status FROM demo_status;
Check result
idstatus
42pending

DEFAULT fills in the status with «pending» when we leave it out. That does not mean an explicit NULL is always replaced by the default.

Check your understanding: What would happen with INSERT INTO demo_status VALUES (43, NULL)?

Show answer

The row would be stored with status set to NULL. DEFAULT only applies when you omit the column; an explicit NULL is kept because this column has no NOT NULL.

How DEFAULT is used

The value is part of the definition of status. The NOT NULL constraint states a different rule: the column cannot be left without a value.

Common mix-ups with DEFAULT

Expecting DEFAULT to replace an explicit NULL

Problem example
INSERT INTO orders_demo (id, status)
VALUES (103, NULL);

Because status is NOT NULL, this insert fails. To get the default, leave the column out or use the DEFAULT syntax your engine supports.

Leaving a value out and sending NULL are different choices

A default applies when the write leaves the column out or uses the engine's syntax for requesting the default. It is not a general rule for filling in any missing input. Sending NULL explicitly usually stores that null or breaks a NOT NULL constraint, depending on the design; it does not request the default.

This matters for imports and forms. A CSV converter may list every column explicitly and write nulls into empty cells. If you want some of them to take the default, define that policy when you build the write, rather than expecting the schema to guess your intent.

Test separately an insert that omits the column, one that supplies it and one that sends a null. A default date or time is usually computed on insert, not updated every time the row changes. Changing the default does not rewrite existing records either: migrating historical data is a separate operation. Combine the NOT NULL rule with domain constraints when a status such as «pending» must be valid as well as automatic.

Technical questions: SQLite: CREATE TABLE and constraints (opens in a new tab).