Sub topic 4
Requiring Values with `NOT NULL`
This chapter assumes you understand tables, rows, columns, basic INSERT and UPDATE statements, and common SQL data types. You should also know that NULL represents a missing or unknown value. It is not the same as an empty string, zero, or FALSE.
What `NOT NULL` Does
A NOT NULL constraint requires every row to contain a value for a column. The database rejects an insert or update that would leave the column as NULL.
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
email VARCHAR(255) NOT NULL,
full_name VARCHAR(100) NOT NULL,
phone VARCHAR(30)
);Here, email and full_name are required. phone is optional because it has no NOT NULL constraint.
The constraint applies when a row is created and whenever it is changed:
INSERT INTO customers (customer_id, email, full_name)
VALUES (1, '[email protected]', 'Ada Lovelace');
-- Rejected: full_name would be NULL.
INSERT INTO customers (customer_id, email)
VALUES (2, '[email protected]');
-- Rejected: the update would make email NULL.
UPDATE customers
SET email = NULL
WHERE customer_id = 1;The first insert succeeds because both required columns have values. The next two statements violate NOT NULL, so the database raises an error and does not apply the invalid change.
`NULL` Is Different from Other Empty Values
A required column must contain a value, but the database does not decide whether that value is meaningful.
| Stored value | Is it `NULL`? | Example meaning |
|---|---|---|
| `NULL` | Yes | No value is known |
| `''` | No | Known empty text |
| `0` | No | Known numeric zero |
| `'Unknown'` | No | Explicit text value |
For example, NOT NULL prevents a missing email, but it does not prevent an empty email:
INSERT INTO customers (customer_id, email, full_name)
VALUES (3, '', 'Unknown Contact');If an empty string is also invalid, use a suitable CHECK constraint or validate the value in application code. NOT NULL handles presence, not content quality.
`NOT NULL` and `DEFAULT`
A DEFAULT supplies a value when an INSERT omits a column. It does not override an explicit NULL.
CREATE TABLE tasks (
task_id INTEGER PRIMARY KEY,
title VARCHAR(200) NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'open'
);
INSERT INTO tasks (task_id, title)
VALUES (1, 'Review report');
-- Uses the default value 'open'.
INSERT INTO tasks (task_id, title, status)
VALUES (2, 'Publish report', NULL); -- RejectedThe first insert succeeds because the omitted status column receives its default. The second explicitly supplies NULL, so NOT NULL rejects it.
Adding `NOT NULL` Later
Adding NOT NULL to an existing table requires every existing row to satisfy the rule. First find and repair missing values, then apply the constraint. Exact syntax varies by database system, but PostgreSQL uses:
ALTER TABLE customers
ALTER COLUMN full_name SET NOT NULL;If any existing full_name value is NULL, this statement fails. Check before changing the schema:
SELECT customer_id
FROM customers
WHERE full_name IS NULL;Exercise
If a column has NOT NULL DEFAULT 'new', what happens when an INSERT omits the column, and what happens when it explicitly supplies NULL?
Show answer
Omitting the column uses 'new'. Explicitly supplying NULL violates NOT NULL and the row is rejected.