Sub topic 13
Constraints: NOT NULL, UNIQUE, and CHECK
Assume you already know basic SELECT, WHERE, ORDER BY, JOIN, and GROUP BY. This chapter focuses on how constraints treat NULL, not on creating tables in general.
A constraint is a rule that the database checks when you insert or update a row. NULL interacts with each constraint differently:
| Constraint | Typical rule involving `NULL` |
|---|---|
| `NOT NULL` | Rejects a `NULL` value |
| `UNIQUE` | Usually permits multiple `NULL` values |
| `CHECK` | Usually accepts `NULL` when its condition becomes `UNKNOWN` |
`NOT NULL` Requires a Value
Use NOT NULL when a row cannot be meaningful without a value. The database rejects both an omitted value and an explicit NULL.
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL,
commission NUMERIC(8, 2)
);
INSERT INTO employees (id, commission)
VALUES (1, NULL); -- Error: name is requiredA default value does not make a column required. If a column has DEFAULT 0 but no NOT NULL, a caller can still explicitly insert NULL.
CREATE TABLE tasks (
id INTEGER PRIMARY KEY,
status VARCHAR(20) NOT NULL DEFAULT 'open',
priority INTEGER DEFAULT 0
);Here, status always has a value. priority receives 0 only when it is omitted. An explicit NULL remains allowed.
`UNIQUE` and Missing Values
A UNIQUE constraint prevents duplicate non-NULL values. In many database systems, it allows more than one NULL, because NULL means that the value is unknown, and two unknown values are not treated as equal.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
email VARCHAR(255) UNIQUE
);
INSERT INTO customers (id, email) VALUES (1, NULL);
INSERT INTO customers (id, email) VALUES (2, NULL); -- Usually allowed
INSERT INTO customers (id, email) VALUES (3, '[email protected]');
INSERT INTO customers (id, email) VALUES (4, '[email protected]'); -- ErrorIf every customer must have a distinct email address, combine the constraints:
CREATE TABLE customer_accounts (
id INTEGER PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE
);Database systems differ in how unique constraints and indexes handle NULL, especially when a constraint covers multiple columns. Check your system's documentation before relying on a particular behavior.
`CHECK` and `UNKNOWN`
A CHECK constraint rejects a row when its condition is FALSE. In standard SQL behavior, a condition that evaluates to TRUE or UNKNOWN passes. Since comparisons with NULL usually produce UNKNOWN, this constraint still permits a missing commission:
CREATE TABLE commissions (
employee_id INTEGER PRIMARY KEY,
amount NUMERIC(8, 2) CHECK (amount >= 0)
);
INSERT INTO commissions (employee_id, amount)
VALUES (1, NULL); -- Usually allowed
INSERT INTO commissions (employee_id, amount)
VALUES (2, -10); -- RejectedRequire both presence and validity by adding NOT NULL:
CREATE TABLE required_commissions (
employee_id INTEGER PRIMARY KEY,
amount NUMERIC(8, 2) NOT NULL CHECK (amount >= 0)
);You can also state the nullable policy directly:
CREATE TABLE optional_commissions (
employee_id INTEGER PRIMARY KEY,
amount NUMERIC(8, 2)
CHECK (amount IS NULL OR amount >= 0)
);This says that NULL is intentionally allowed, while present values must be nonnegative.
Exercise
What constraints should you use for a column that must contain a nonnegative, unique account number?
Show answer
Use NOT NULL UNIQUE CHECK (account_number >= 0). NOT NULL rejects missing values, UNIQUE rejects duplicate values, and CHECK rejects negative values.