Part 37: DEFAULT Values & Generated / Computed Columns

Part 37 Technology Updated September 12, 2026

Part 37: DEFAULT Values & Generated / Computed Columns

Generated columns calculate their value automatically from other columns in the row.


1. Virtual vs Stored Generated Columns

  • STORED: Calculated on write/update and stored on disk. Can be indexed!
  • VIRTUAL (MySQL): Calculated on read on the fly.
CREATE TABLE order_items (
  id BIGSERIAL PRIMARY KEY,
  quantity INT NOT NULL,
  unit_price DECIMAL(10, 2) NOT NULL,

  -- Computed total:
  line_total DECIMAL(12, 2) GENERATED ALWAYS AS (quantity * unit_price) STORED
);

Key Takeaways

  • Use generated columns to guarantee derived fields stay synchronized with source data.

In Part 38, we will master Modifying Tables: ALTER TABLE!