NOT NULL and CHECK Constraints
Forbid missing values with NOT NULL and validate domain rules with CHECK (price > 0, status IN ('active','archived')).
Why Constraints?
Constraints push validation INTO the database. The benefit is huge: every app, script, and ad-hoc query writes valid data — or fails loudly.
NOT NULL
The simplest constraint: this column must have a value:
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
email VARCHAR(255) NOT NULL,
age INT -- NULL allowed
);All lessons in this course
- NOT NULL and CHECK Constraints
- UNIQUE Constraints and Composite Keys
- FOREIGN KEY and Referential Actions
- DEFAULT Values and Generated Columns