Example
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
UPDATE generated_lines
SET quantity = 4
WHERE id = 1;
SELECT id, quantity, ROUND(subtotal, 2) AS subtotal
FROM generated_lines
WHERE id = 1;| id | quantity | subtotal |
|---|---|---|
| 1 | 4 | 99,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
| Option | Where the logic lives | When it fits |
|---|---|---|
| Generated column | In the table definition | The value must always be derived from the row |
DEFAULT | In the table, only when no value is supplied | You need an initial value, not a permanent formula |
VIEW | In a reusable query | The calculation belongs to a broader logical view |
Expression in SELECT | In one specific query | The 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.
| Engine | Name / usual form |
|---|---|
| PostgreSQL | GENERATED ALWAYS AS (...) STORED | VIRTUAL |
| MySQL | generated column AS (...), virtual or stored |
| SQLite | GENERATED ALWAYS AS (...), VIRTUAL or STORED |
| SQL Server | column_name AS expression, optionally PERSISTED |
The permitted expressions, indexing, nullability, constraints and ALTER TABLE operations differ. Design against the documentation for your target engine.