0Pricing
SQL Academy · Lesson

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

  1. NOT NULL and CHECK Constraints
  2. UNIQUE Constraints and Composite Keys
  3. FOREIGN KEY and Referential Actions
  4. DEFAULT Values and Generated Columns
← Back to SQL Academy