Concept · Designing databases

Generated columns in SQL: GENERATED ALWAYS AS

A generated column computes its value from other columns in the same row, using an expression defined in the table.

Example

DDL + data
CREATE TEMP TABLE generated_lines (
  id INTEGER PRIMARY KEY,
  quantity INTEGER NOT NULL,
  unit_price REAL NOT NULL,
  subtotal REAL
    GENERATED ALWAYS AS (quantity * unit_price) STORED
);

INSERT INTO generated_lines (id, quantity, unit_price)
VALUES (1, 2, 24.90),
       (2, 3, 49.50);

SELECT id, quantity, unit_price,
       ROUND(subtotal, 2) AS subtotal
FROM generated_lines
ORDER BY id;

The INSERT does not provide subtotal. The column's expression computes it from the underlying data.

How it is used

If quantity = 2 and unit_price = 24.90, the subtotal can be derived from those two values. Storing an editable subtotal as well creates the risk that it stops matching its components. A generated column records that dependency in the schema.

Design question: does the value have a life of its own, or must it always be the result of other columns in the same row? If it is always derived and the engine supports it, a generated column may be a good option.

What changes from case to case

Change the inputs and the derived value changes

Verified UPDATE
UPDATE generated_lines
SET quantity = 4
WHERE id = 1;

SELECT id, quantity, ROUND(subtotal, 2) AS subtotal
FROM generated_lines
WHERE id = 1;
Result after UPDATE
idquantitysubtotal
1499,60

The subtotal changes from 49.80 to 99.60 because it depends on quantity. You do not need to keep two values in sync by hand.

Generated column vs DEFAULT, VIEW and a calculation in SELECT

Tools for derived values
OptionWhere the logic livesWhen it fits
Generated columnIn the table definitionThe value must always be derived from the row
DEFAULTIn the table, only when no value is suppliedYou need an initial value, not a permanent formula
VIEWIn a reusable queryThe calculation belongs to a broader logical view
Expression in SELECTIn one specific queryThe calculation is only needed in that result

Avoid this confusion

Trying to insert the derived value by hand

The whole point of the column is that the engine works out the result from its expression. Insert or change the underlying columns; do not maintain a second copy by hand.

Using a generated column for logic that needs other rows

These expressions are built around the values of the row itself and are subject to engine restrictions. For aggregates, subqueries or broader logic, you may need a query, a view or another approach.

Assuming STORED, VIRTUAL and PERSISTED mean exactly the same thing

They describe choices about computation and storage, but they belong to different implementations. Do not copy DDL from one engine to another without checking the syntax and limitations.

Key differences between engines

PostgreSQL, MySQL and SQLite use the concept of generated column with expression-based syntax AS (...), with storage options that depend on the engine. SQL Server calls the feature computed column and offers PERSISTED to store certain computed results physically.

Rough terminology
EngineName / usual form
PostgreSQLGENERATED ALWAYS AS (...) STORED | VIRTUAL
MySQLgenerated column AS (...), virtual or stored
SQLiteGENERATED ALWAYS AS (...), VIRTUAL or STORED
SQL Servercolumn_name AS expression, optionally PERSISTED

The permitted expressions, indexing, nullability, constraints and ALTER TABLE operations differ. Design against the documentation for your target engine.