← Lessons

quiz vs the machine

Silver1100

Databases

Generated and Computed Columns

Columns whose value is derived from other columns by a rule.

3 min read · intro · beat Silver to climb

A Column from a Formula

A generated column, also called a computed column, holds a value derived from other columns by an expression you define. You never insert into it directly. The database fills it from the rule, like a full name built from first and last name.

Stored vs Virtual

  • A stored generated column saves the computed value on disk, so reads are fast but writes do a little extra work.
  • A virtual generated column computes the value on each read, saving space but spending time at query time.

Why They Help

  • They keep derived data consistent, since the rule lives in one place.
  • A stored generated column can be indexed, so you can search on the derived value quickly.
  • They simplify queries because the calculation is defined once.

A Limit

The expression usually must be deterministic and may only reference columns in the same row, so it cannot pull from other tables or use random values.

Key idea

A generated column derives its value from other columns by a rule, keeping data consistent and, when stored, allowing the derived value to be indexed.

Check yourself

Answer to earn rating on the learn ladder.

1. What is the difference between a stored and a virtual generated column?

2. What restriction usually applies to a generated column expression?