Concept · Designing databases

NOT NULL

NOT NULL stops a NULL value from being stored in a column. On its own it does not block an empty string or check that the value is correct.

Before continuing: This entry is part of the constraints and keys guide. If you are new to the topic, start with CREATE TABLE.

NOT NULL example

Required name
CREATE TABLE products_demo (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
INSERT INTO products_demo (id, name)
VALUES (1, 'Keyboard');

An INSERT that supplies NULL for name is rejected. An empty string is still text and needs an extra rule if it is not valid.

What to look for: If you try to save a new row with name = NULL, the operation fails because of the constraint. An empty string, however, is not NULL: if you want to block it too, you need to check for that case separately.

Check the result with small data

The table requires a name and the insert supplies a non-null value.

Self-contained test · SQLite
CREATE TABLE demo_required (id INTEGER, name TEXT NOT NULL);
INSERT INTO demo_required VALUES (1, 'Amy');
SELECT id, name FROM demo_required;
Check result
idname
1Amy

If you change «Amy» to NULL, the INSERT fails because of the NOT NULL constraint. The constraint protects the stored data; it is no substitute for validating input.

Check your understanding: How does this constraint treat NULL differently from an empty string?

Show answer

NULL is a missing value and the constraint rejects it. An empty text ('') is a present value of length 0, so NOT NULL accepts it.

How NOT NULL is used

Declare NOT NULL when a row cannot be understood without that value. Before adding it to an existing table, find and fix the rows that already have missing values.

NOT NULL and IS NOT NULL do different jobs. The constraint governs future writes; the filter IS NOT NULL selects rows while a query runs.

Common mix-ups with NOT NULL

Making every column required

A constraint should reflect a real rule. A second phone number or an internal note can be optional. Forcing placeholder values such as “N/A” makes filtering, statistics and spotting missing data harder.

NOT NULL requires a value, not a useful one

NOT NULL stops a missing value from being stored in a column. It guarantees the value is present and simplifies queries, because filters and calculations no longer have to handle the unknown case. It suits data that must always exist, such as an identifier or a name.

It does not validate the content. An empty string, a space or a placeholder such as “no data” all satisfy the constraint. To block them you need an extra rule, such as CHECK (name <> ''), or validation in the application. In Oracle, an empty string is also stored as null, so behaviour differs between engines.

Add NOT NULL to a table that already has data requires that no row is missing the value, or that a default is defined. Check the existing data first by counting the nulls. Test with one row with no value, one with an empty string and one valid row.

Self-contained case to check in SQLite

The table requires a name; Amy has no city (which is allowed) and the second customer has an empty name.

Data and query · SQLite
CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT NOT NULL, city TEXT);
INSERT INTO customers VALUES (1, 'Amy', NULL), (2, '', 'Leeds');
SELECT COUNT(*) AS rows, COUNT(city) AS with_city, SUM(name = '') AS empty_names FROM customers;
Verified result of the self-contained case
rowswith_cityempty_names
211

NOT NULL accepts the empty name because it is not null. COUNT(city) skips the missing value and counts only one city.

Check your interpretation: Which extra constraint would stop the empty name from being saved?

Show answer

A CHECK on the content, for example CHECK (name <> ''), or CHECK (TRIM(name) <> '') if you also want to reject a name made only of spaces.

Technical questions: SQLite: CREATE TABLE and constraints (opens in a new tab).