DEFAULT Values and Generated Columns
Provide DEFAULTs (NOW(), uuid_generate_v4(), 'pending') and compute values on the fly with GENERATED ALWAYS AS columns.
DEFAULT: Auto-Fill Missing Values
When INSERT omits a column, the DEFAULT is used:
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
email VARCHAR(255) NOT NULL,
status TEXT NOT NULL DEFAULT 'active',
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);Common DEFAULTs
Patterns you'll use everywhere:
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
id UUID PRIMARY KEY DEFAULT gen_random_uuid()
is_active BOOLEAN NOT NULL DEFAULT true
status TEXT NOT NULL DEFAULT 'pending'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