Skip to main content

How to Store a Missing Value in SQLite with NULL

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Not every value is known: a contact may have no phone number. SQLite stores a missing value as NULL, which isn't zero or empty text but no value at all. This guide stores two contacts without a phone, with real output from SQLite 3.54.1.

Syntax​

Syntax
INSERT INTO <table> (<column 1>, <column 2>) VALUES (<value>, NULL);
PartWhat it means
NULLThe missing value, written without quotes
A column left out of the INSERTAlso gets NULL, when it has no default value
typeof(<column>)Returns the type of each value: null for a missing one

SQLite's documentation on typeof(): "The typeof(X) function returns a string that indicates the datatype of the expression X: "null", "integer", "real", "text", or "blob"."

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 null_value.sql:

null_value.sql
CREATE TABLE contacts (
name TEXT NOT NULL,
phone TEXT
);

INSERT INTO contacts (name, phone) VALUES ('Ana', '555-0101');
INSERT INTO contacts (name, phone) VALUES ('Ben', NULL);
INSERT INTO contacts (name) VALUES ('Cleo');

SELECT name, phone, typeof(phone) FROM contacts;

The null_value.sql script in a macOS-style code window: a contacts table with name and phone, an insert of Ana with a phone, one of Ben with NULL as the phone, one of Cleo without a phone, and a SELECT of name, phone and typeof(phone).

  • Ana has a phone number.
  • Ben's phone is written as NULL, without quotes.
  • Cleo's INSERT leaves the phone out.
  • typeof(phone) shows the type of each stored phone.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table contacts.db ".read null_value.sql"

sqlite3 opens the database file contacts.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
+------+----------+---------------+
| name | phone | typeof(phone) |
+------+----------+---------------+
| Ana | 555-0101 | text |
| Ben | | null |
| Cleo | | null |
+------+----------+---------------+

The output in a terminal: the contacts table with Ana's phone 555-0101 of type text, and Ben and Cleo with empty phones of type null.

Ben's and Cleo's phones are empty cells, and typeof says null for both: writing NULL and leaving the column out stored the same thing. Ana's phone is text.

Common Mistakes​

MistakeWhat happensDo this instead
Writing 'NULL' in quotesThe four letters NULL were stored as text, and typeof said textWrite NULL without quotes
Calculating with a missing valueNULL + 1 and 'Ben' || NULL both gave NULLSwap in a value first: coalesce(NULL, 0) + 1 gave 1

Troubleshooting​

SymptomLikely causeFix
A cell shows nothingThe value is NULL, which -table prints as an empty cellRun with -nullvalue NULL to print the word NULL instead
count(phone) is smaller than count(*)count(<column>) skips NULL valuesUse count(*) to count rows
Parse error near line 1 of null_value.sql: table contacts already existsThe script already ran on contacts.dbDelete contacts.db and run the script again

FAQ​

How do I tell a missing value from empty text?​

With typeof(): it returned null for NULL and text for ''. In the shell, -nullvalue NULL also helps: sqlite3 -table -nullvalue NULL contacts.db prints NULL in every cell that has no value.

Does count() count missing values?​

count(*) counts rows, and count(phone) counts only the phones that are there. On this table they returned 3 and 1.

What does coalesce() do?​

It returns its first argument that isn't NULL. The documentation: "The coalesce() function returns a copy of its first non-NULL argument, or NULL if all arguments are NULL."

Conclusion​

NULL, written without quotes or by leaving the column out, stored Ben's and Cleo's missing phones, and typeof(phone) showed null for both.

Additional Resources​