← Back to SQL, page 3

sql-0045 · Medium mid

Should a column allow NULL? How do you decide when designing a table?

Make a column NOT NULL unless a missing value is a real, expected state with a clear meaning. NULL then means one thing, such as 'not known yet' or 'not applicable'. If a column would mean several different things when empty, or every query has to remember to handle it, a default value or a separate table is usually better.

Full answer

Every nullable column adds work for every query that touches it: three-valued logic in filters, special handling in averages and joins, and more cases in application code. So the default should be NOT NULL, and NULL should be allowed on purpose.

Questions to ask for each column:

  1. Can a valid row exist without this value? An order cannot exist without an order date. A batch of students can exist before its end date is known.
  2. If the value is missing, what does that mean? If the answer is "not known yet", NULL fits. If it could mean either "unknown" or "not applicable", the column needs a clearer design.
  3. Is there a natural default? A status column can default to 'planned'. A counter can default to 0. Then NOT NULL DEFAULT is better than NULL.
  4. Is the value really optional data about a different thing? A user's optional company details may belong in a separate table that has a row only when the details exist.

A small table that follows these rules:

sql
CREATE TABLE batches (
  id          INTEGER PRIMARY KEY,
  course      TEXT    NOT NULL,
  start_date  TEXT    NOT NULL,
  end_date    TEXT,                                   -- NULL means still running
  status      TEXT    NOT NULL DEFAULT 'planned',
  seats_taken INTEGER NOT NULL DEFAULT 0 CHECK (seats_taken >= 0),
  CHECK (end_date IS NULL OR end_date >= start_date)
);
SELECT name, "notnull" AS not_null, dflt_value FROM pragma_table_info('batches') ORDER BY cid;
namenot_nulldflt_value
id0NULL
course1NULL
start_date1NULL
end_date0NULL
status1'planned'
seats_taken10

Inserting a row with only the required fields shows the defaults and the deliberate NULL:

sql
INSERT INTO batches (course, start_date) VALUES ('SQL Basics', '2026-04-01');
SELECT id, course, start_date, end_date, status, seats_taken FROM batches;
idcoursestart_dateend_datestatusseats_taken
1SQL Basics2026-04-01NULLplanned0

end_date stays NULL on purpose and means "still running". status and seats_taken get real defaults rather than NULLs that every query would have to translate.

sql
CREATE TABLE batches (
  id          INTEGER PRIMARY KEY,
  course      TEXT    NOT NULL,
  start_date  TEXT    NOT NULL,
  end_date    TEXT,                                   -- NULL means still running
  status      TEXT    NOT NULL DEFAULT 'planned',
  seats_taken INTEGER NOT NULL DEFAULT 0 CHECK (seats_taken >= 0),
  CHECK (end_date IS NULL OR end_date >= start_date)
);
SELECT name, "notnull" AS not_null, dflt_value FROM pragma_table_info('batches') ORDER BY cid;

INSERT INTO batches (course, start_date) VALUES ('SQL Basics', '2026-04-01');
SELECT id, course, start_date, end_date, status, seats_taken FROM batches;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Default to NOT NULL; allow NULL deliberately
  • NULL should have one clear meaning per column
  • Prefer NOT NULL DEFAULT when a natural default exists
  • Optional groups of attributes may belong in a separate table

Common mistakes

  • Leaving every column nullable because the ORM does by default
  • Using NULL to mean both 'unknown' and 'not applicable' in one column

Follow-ups

  • How do you add NOT NULL to a column that already has NULLs?
  • What is the cost of many nullable columns for queries?

Tips

  • Walk through two or three columns of a concrete table instead of answering in the abstract