0
0
PostgreSQLquery~10 mins

GENERATED columns (stored and virtual) in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - GENERATED columns (stored and virtual)
Create Table
Define Columns
Define Regular Columns
Define GENERATED Column
Specify Expression
Choose STORED or VIRTUAL
Insert Data
GENERATED Column Value Computed
Query Table
See Computed Values
When creating a table, you define normal columns and GENERATED columns with expressions. The GENERATED column computes its value automatically, either stored physically or computed on the fly.
Execution Sample
PostgreSQL
CREATE TABLE products (
  price numeric,
  quantity int,
  total_cost numeric GENERATED ALWAYS AS (price * quantity) STORED
);
Creates a table with a stored generated column that calculates total_cost as price times quantity.
Execution Table
StepActionInput DataGenerated Column ComputationResulting Row
1Insert rowprice=10, quantity=3total_cost = 10 * 3 = 30(10, 3, 30)
2Insert rowprice=5, quantity=7total_cost = 5 * 7 = 35(5, 7, 35)
3Query tableN/AValues retrieved from stored column(10, 3, 30), (5, 7, 35)
4Update priceprice=6 for first rowtotal_cost recalculated = 6 * 3 = 18(6, 3, 18)
5Query tableN/AUpdated stored values shown(6, 3, 18), (5, 7, 35)
6Insert row with NULLprice=NULL, quantity=4total_cost = NULL * 4 = NULL(NULL, 4, NULL)
7Query tableN/ANULL shown for total_cost(6, 3, 18), (5, 7, 35), (NULL, 4, NULL)
8Attempt to insert invalid dataprice='abc', quantity=2Error: invalid input for numericInsert fails
💡 Execution stops after error on invalid data insert.
Variable Tracker
VariableStartAfter 1After 2After 4After 6Final
priceN/A1056NULL6
quantityN/A37343
total_costN/A303518NULL18
Key Moments - 3 Insights
Why does total_cost update automatically when price changes?
Because total_cost is a GENERATED column defined as price * quantity, it recalculates whenever price or quantity changes, as shown in execution_table row 4.
What happens if one of the input columns is NULL?
The generated column result becomes NULL because any arithmetic with NULL yields NULL, as seen in execution_table row 6.
Why does inserting a non-numeric value for price cause an error?
Because price is numeric type, inserting 'abc' is invalid and the database rejects it, shown in execution_table row 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is total_cost after the first insert?
A13
B30
C3
DNULL
💡 Hint
Check execution_table row 1 under Generated Column Computation.
At which step does total_cost become NULL?
AStep 6
BStep 2
CStep 4
DStep 8
💡 Hint
Look at execution_table row 6 where price is NULL.
If total_cost was defined as VIRTUAL instead of STORED, what would change?
AValues would be stored physically in the table.
BYou could insert values manually into total_cost.
CValues would be computed on query, not stored.
DThe table would reject NULL values.
💡 Hint
Recall the difference between STORED and VIRTUAL generated columns.
Concept Snapshot
GENERATED columns compute values automatically from other columns.
Syntax: column_name data_type GENERATED ALWAYS AS (expression) STORED|VIRTUAL.
STORED saves computed values physically; VIRTUAL computes on query.
Useful for derived data like totals or concatenations.
Cannot insert or update generated columns directly.
Full Transcript
This lesson shows how GENERATED columns work in MySQL. When creating a table, you can define columns that automatically calculate their values from other columns using an expression. These columns can be STORED, meaning their values are saved in the table, or VIRTUAL, meaning their values are calculated when you query the table. For example, a total_cost column can be generated as price times quantity. When you insert or update rows, the database automatically calculates total_cost. If input columns are NULL, the generated column becomes NULL. Invalid data types cause errors. This visual trace shows step-by-step how data is inserted, how generated columns compute values, and how updates affect them.