Skip to main content

How to Give a Column a Default Value in SQLite with DEFAULT

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Some columns have an obvious starting value: a new task isn't done yet. DEFAULT gives a column that value, so an INSERT can leave the column out. This guide starts every task at 0, not done, with real output from SQLite 3.54.1.

Syntax​

Syntax
CREATE TABLE <table> (
<column> <type> DEFAULT <value>
);
PartWhat it means
DEFAULT <value>The value SQLite stores when an INSERT doesn't give one for this column
<value>A number, text in single quotes, NULL, or an expression in parentheses, such as (date('now'))

SQLite's documentation on the DEFAULT clause: "The DEFAULT clause specifies a default value to use for the column if no value is explicitly provided by the user when doing an INSERT."

Prerequisites​

  • The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run sqlite3 --version to see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.

Step 1: Write the Script​

Save this as default_value.sql:

default_value.sql
CREATE TABLE tasks (
title TEXT NOT NULL,
done INTEGER NOT NULL DEFAULT 0
);

INSERT INTO tasks (title) VALUES ('Buy milk');
INSERT INTO tasks (title, done) VALUES ('Pay rent', 1);

SELECT * FROM tasks;

The default_value.sql script in a macOS-style code window: a tasks table whose done column is INTEGER NOT NULL DEFAULT 0, an insert that gives only a title, one that gives a title and done as 1, and a SELECT of every row.

  • done INTEGER NOT NULL DEFAULT 0 starts every task at 0, not done.
  • The first INSERT lists only title, so done gets the default.
  • The second gives done a value of its own, 1.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table todo.db ".read default_value.sql"

sqlite3 opens the database file todo.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.

Output
+----------+------+
| title | done |
+----------+------+
| Buy milk | 0 |
| Pay rent | 1 |
+----------+------+

The output in a terminal: the tasks table with Buy milk at done 0 and Pay rent at done 1.

Buy milk got 0 without its INSERT mentioning done. Pay rent gave its own value, 1, and kept it: a default only fills a column that the INSERT leaves out.

Common Mistakes​

MistakeWhat happensDo this instead
Giving the column NULL, as in VALUES ('Buy milk', NULL)NOT NULL constraint failed: tasks.done. An explicit NULL is a value, so SQLite doesn't use the defaultLeave the column out of the INSERT
A function call without parentheses, DEFAULT date('now')Parse error ...: near "(": syntax errorWrap it in parentheses: DEFAULT (date('now'))

Troubleshooting​

SymptomLikely causeFix
NOT NULL constraint failed: tasks.doneThe INSERT gave done the value NULLLeave done out, or give it 0 or 1
Parse error near line 1 of default_value.sql: table tasks already existsThe script already ran on todo.dbDelete todo.db and run the script again

FAQ​

Can a default be today's date?​

Yes, as an expression in parentheses: added_on TEXT DEFAULT (date('now')). A row inserted without added_on got today's date.

What's a column's default when it has no DEFAULT?​

NULL. The documentation: "If there is no explicit DEFAULT clause attached to a column definition, then the default value of the column is NULL."

Conclusion​

done INTEGER NOT NULL DEFAULT 0 gave Buy milk a 0 without the INSERT naming the column, and Pay rent kept the 1 it was given.

Additional Resources​