Skip to main content

How to Make a Column Required in SQLite with NOT NULL

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Some values a row can't do without: a contact needs a name, even if the phone number is unknown. NOT NULL tells SQLite which columns are required, and it refuses any row that leaves one out. This guide makes a contact's name required, with real output from SQLite 3.54.1.

Syntax​

Syntax
CREATE TABLE <table> (
<column> <type> NOT NULL,
<optional column> <type>
);
PartWhat it means
<column> <type> NOT NULLEvery row must have a value in this column
<optional column> <type>Without NOT NULL, the column may be left empty, which SQLite stores as NULL

SQLite's documentation: "a NOT NULL constraint dictates that the associated column may not contain a NULL value."

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

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

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

SELECT * FROM contacts;

The not_null.sql script in a macOS-style code window: a contacts table with name TEXT NOT NULL and phone TEXT, an insert with both values, one with only a name, one with only a phone, and a SELECT of every row.

  • name TEXT NOT NULL makes the name required. phone TEXT is optional.
  • Ana has both values, Ben has only a name, and the third row has only a phone.
  • SELECT * FROM contacts shows which rows went in.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table contacts.db ".read not_null.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
Error near line 8 of not_null.sql: NOT NULL constraint failed: contacts.name
+------+----------+
| name | phone |
+------+----------+
| Ana | 555-0101 |
| Ben | |
+------+----------+

The output in a terminal: an error near line 8 of not_null.sql, NOT NULL constraint failed: contacts.name, then the contacts table with Ana and her phone 555-0101, and Ben with an empty phone.

Ben went in without a phone: the column is optional, so it holds NULL, which the table shows as empty. Line 8 left out the name, and SQLite refused that row with NOT NULL constraint failed: contacts.name. The shell reported it and carried on to the SELECT.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting NOT NULL to refuse empty text'' is a value, not NULL: a contact with the name '' went inAdd a check as well: name TEXT NOT NULL CHECK (name <> '') refused it with CHECK constraint failed: name <> ''
Adding a NOT NULL column to a table that already has rowsCannot add a NOT NULL column with default value NULLGive the new column a default, such as ALTER TABLE contacts ADD COLUMN email TEXT NOT NULL DEFAULT ''

Troubleshooting​

SymptomLikely causeFix
NOT NULL constraint failed: contacts.nameThe INSERT left the column out, or gave it NULLGive every required column a value
Parse error near line 1 of not_null.sql: table contacts already existsThe script already ran on contacts.dbDelete contacts.db and run the script again

FAQ​

Can one NOT NULL cover several columns?​

No. It goes on each column's own line. The documentation: "A NOT NULL constraint may only be attached to a column definition, not specified as a table constraint."

Can a required column be left out of an INSERT?​

Only when it has a default value. With done INTEGER NOT NULL DEFAULT 0, a row that leaves out done gets 0 and goes in.

Conclusion​

name TEXT NOT NULL let Ben in without a phone, and refused the row with no name: NOT NULL constraint failed: contacts.name.

Additional Resources​