Skip to main content

Default Values

DEFAULT gives a column an automatic value when no explicit value is provided.

This keeps inserts simpler and creates predictable baseline data.

Core Concepts

Default typeExampleCommon use
Literal textDEFAULT 'draft'Status fields
Numeric literalDEFAULT 0Flags/counters
Timestamp expressionDEFAULT CURRENT_TIMESTAMPCreated time
CREATE TABLE tasks (
task_id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
status TEXT DEFAULT 'todo',
priority INTEGER DEFAULT 1
);
Expected output
Table created with text and numeric default values.

Code Examples

-- Inspect defaults in table metadata.
PRAGMA table_info(tasks);
cidnametypenotnulldflt_valuepk
0task_idINTEGER0null1
1titleTEXT1null0
2statusTEXT0'todo'0
3priorityINTEGER010

SQLite-Specific Nuances

SQLite Nuance

A default is used only when the column is omitted in an insert statement.

If a query explicitly supplies NULL, SQLite treats that as an explicit value.

Common Pitfalls / Best Practices

Pitfall

Assuming defaults are retroactively applied to existing rows.

Defaults affect row creation behavior, not automatic backfills.

Best Practice

Use defaults for common initial states (active = 1, status = 'pending') to reduce repeated SQL.

Quick Challenge

Create an issues table with:

  • issue_id primary key
  • title required
  • state default 'open'
  • created_at default CURRENT_TIMESTAMP
View Solution
CREATE TABLE issues (
issue_id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
state TEXT DEFAULT 'open',
created_at TEXT DEFAULT CURRENT_TIMESTAMP
);
Expected output
Table created with both literal and expression defaults.