Sub topic 14
Design Choices: When to Allow NULL
Assume you know basic SELECT, WHERE, JOIN, and GROUP BY, and that you have seen how SQL comparisons and aggregates treat NULL.
A column should allow NULL when a missing value has a meaningful interpretation. NULL can mean “not known yet,” “not provided,” or “does not apply.” These meanings are different from zero, an empty string, and FALSE, so do not use NULL merely because a value is inconvenient to collect.
Choose `NULL` when absence is valid
Consider a customer profile:
| Column | Possible meaning of `NULL` | Better design |
|---|---|---|
| `middle_name` | The person has no middle name or did not provide one | Allow `NULL` if those cases need no distinction |
| `delivery_instructions` | No instructions were supplied | Allow `NULL` or use an empty value by policy |
| `birth_date` | The date is unknown | Allow `NULL` |
| `is_active` | The application does not know the status | Usually do not allow `NULL`; require `TRUE` or `FALSE` |
| `quantity` | No quantity was recorded | Usually do not allow `NULL`; use a required value or model the event separately |
A nullable column adds a third state to the data. For example, is_active can become TRUE, FALSE, or UNKNOWN. That extra state often causes incorrect filters and reports. If the business rule has only two states, declare the column NOT NULL.
Use a default when a value has a reliable, universal meaning at insertion time. A default is not a substitute for missing information. For example, a new account might receive is_active = TRUE, but an unknown customer age should not receive 0.
CREATE TABLE accounts (
account_id INTEGER PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE,
is_active BOOLEAN NOT NULL DEFAULT TRUE,
birth_date DATE NULL,
marketing_opt_in BOOLEAN NOT NULL DEFAULT FALSE
);Here, birth_date may be unknown. The status and opt-in decision must always have a value.
Use a separate table for repeating or changing facts
A nullable column works poorly when one entity can have many values, or when each value needs its own history and metadata. Do not add columns such as phone_1, phone_2, and phone_3. Store phones in a related table instead.
CREATE TABLE account_phones (
phone_id INTEGER PRIMARY KEY,
account_id INTEGER NOT NULL,
phone_number VARCHAR(30) NOT NULL,
phone_type VARCHAR(20) NOT NULL,
is_primary BOOLEAN NOT NULL DEFAULT FALSE,
FOREIGN KEY (account_id) REFERENCES accounts(account_id)
);An account with no phone has no rows in account_phones. An account with three phones has three rows. This design avoids several NULL columns and supports unlimited phones.
Preserve distinctions that matter
If “not applicable,” “not yet measured,” and “user declined” lead to different decisions, one NULL value cannot represent all three states. Use a status column, a reason code, or a separate table.
For example, an optional survey answer might need:
answer_value, which can beNULLanswer_status, such asnot_presented,declined, oranswered
This makes reporting explicit instead of forcing every query to guess what NULL means.
Review a nullable column
Before allowing NULL, ask:
- What exact situation does
NULLrepresent? - Is that situation different from zero, empty text, or
FALSE? - Should queries treat “missing” as a separate reporting category?
- Could a default provide a correct value?
- Can the value repeat, change independently, or require metadata?
Exercise
Should an employee's commission column allow NULL when NULL means “this employee has no commission plan,” while zero means “the plan exists but produced no commission”?
Show answer
Yes. NULL and zero represent different business states. Document that meaning, and use COALESCE only in reports that intentionally display missing commission as zero.