How to Make a Column Required in SQLite with NOT NULL
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
CREATE TABLE <table> (
<column> <type> NOT NULL,
<optional column> <type>
);
| Part | What it means |
|---|---|
<column> <type> NOT NULL | Every 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 --versionto 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:
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;

name TEXT NOT NULLmakes the name required.phone TEXTis optional.- Ana has both values, Ben has only a name, and the third row has only a phone.
SELECT * FROM contactsshows which rows went in.
Step 2: Run It
In the folder that holds the script, run:
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.
Error near line 8 of not_null.sql: NOT NULL constraint failed: contacts.name
+------+----------+
| name | phone |
+------+----------+
| Ana | 555-0101 |
| Ben | |
+------+----------+

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
| Mistake | What happens | Do this instead |
|---|---|---|
Expecting NOT NULL to refuse empty text | '' is a value, not NULL: a contact with the name '' went in | Add 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 rows | Cannot add a NOT NULL column with default value NULL | Give the new column a default, such as ALTER TABLE contacts ADD COLUMN email TEXT NOT NULL DEFAULT '' |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
NOT NULL constraint failed: contacts.name | The INSERT left the column out, or gave it NULL | Give every required column a value |
Parse error near line 1 of not_null.sql: table contacts already exists | The script already ran on contacts.db | Delete 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
- NOT NULL constraints — what the constraint checks.
- CREATE TABLE — every column constraint.
