âī¸ Generated Columns are table columns whose values are automatically computed from other columns using an expression. Instead of storing manually entered values, the database calculates them whenever a row is inserted or updated, helping reduce data duplication and maintain consistency.
đ What are Generated Columns?
A generated column derives its value from one or more existing columns in the same table. For example, if a table stores the quantity and unit price of a product, a generated column can automatically calculate the total price.
Information
đ¯ Why Use Generated Columns?
Generated columns eliminate repetitive calculations and ensure derived values remain accurate.
- đ Automatically calculate values.
- đ Reduce duplicate data.
- đ Improve data consistency.
- đ Simplify SQL queries.
- đ Minimize application-side calculations.
đ Types of Generated Columns
| Type | Description |
|---|---|
| Virtual Generated Column | Calculated whenever the value is read. Usually consumes little or no additional storage. |
| Stored Generated Column | Calculated when data is inserted or updated and stored in the table. |
Important
đ Basic Syntax (MySQL)
Generated Column Syntax
CREATE TABLE table_name
(
column1 data_type,
column2 data_type,
generated_column data_type
GENERATED ALWAYS AS (expression)
VIRTUAL
);đĄ Create a Table with a Generated Column
Create a Products table where the total price is calculated automatically from the quantity and unit price.
Virtual Generated Column
CREATE TABLE Products
(
ProductID INT PRIMARY KEY,
Quantity INT,
UnitPrice DECIMAL(10,2),
TotalPrice DECIMAL(10,2)
GENERATED ALWAYS AS
(Quantity * UnitPrice) VIRTUAL
);â Insert Data
Notice that the generated column is not included in the INSERT statement.
Insert Product
INSERT INTO Products
(ProductID, Quantity, UnitPrice)
VALUES
(101, 5, 120.00);đ Result
| ProductID | Quantity | UnitPrice | TotalPrice |
|---|---|---|---|
| 101 | 5 | 120.00 | 600.00 |
đž Stored Generated Columns
A stored generated column calculates its value during data modification and saves the computed value in the table.
Stored Generated Column
CREATE TABLE Products
(
ProductID INT PRIMARY KEY,
Quantity INT,
UnitPrice DECIMAL(10,2),
TotalPrice DECIMAL(10,2)
GENERATED ALWAYS AS
(Quantity * UnitPrice) STORED
);Stored generated columns may improve query performance for frequently accessed calculated values because the result is already stored.
đ Automatic Updates
Whenever one of the source columns changes, the generated column is updated automatically by the database.
Update Source Data
UPDATE Products
SET Quantity = 8
WHERE ProductID = 101;The database automatically recalculates TotalPrice without requiring an additional update statement.
đ Virtual vs Stored Generated Columns
| Feature | Virtual | Stored |
|---|---|---|
| Stored on Disk | â Usually No | â Yes |
| Calculated During Read | â Yes | â No |
| Consumes Storage | Minimal or None | More |
| Read Performance | May require recalculation | Usually Faster |
| Write Performance | Usually Faster | May be Slightly Slower |
đŧ Real-World Example
An invoicing system stores the quantity and unit price of each product. A generated column automatically calculates the line total, ensuring invoices always display accurate amounts without requiring application-side calculations.
Invoice Items
CREATE TABLE InvoiceItems
(
ItemID INT PRIMARY KEY,
Quantity INT,
UnitPrice DECIMAL(10,2),
LineTotal DECIMAL(10,2)
GENERATED ALWAYS AS
(Quantity * UnitPrice) STORED
);đī¸ Database Compatibility
| Database System | Generated Column Support |
|---|---|
| MySQL | Supports VIRTUAL and STORED generated columns. |
| PostgreSQL | Supports stored generated columns using GENERATED ALWAYS AS (... ) STORED. |
| SQL Server | Supports computed columns, with optional persistence. |
| Oracle | Supports virtual columns and related features. |
| SQLite | Supports generated columns (virtual and stored) in modern versions. |
â ī¸ Common Mistakes
- â Trying to manually insert values into generated columns.
- â Using unsupported expressions for generated columns.
- â Choosing a stored generated column when a virtual one would be sufficient.
- â Assuming every database system supports identical syntax and features.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ Generated columns are calculated automatically by the database.
- đ They reduce duplicate data and improve consistency.
- đ Virtual columns are typically calculated when queried.
- đ Stored generated columns save the calculated value in the table.
- đ The exact syntax and capabilities vary across database systems.
- đ Generated columns simplify queries and reduce application-side calculations.