Generated Columns in MySQL

Generated columns in MySQL are a powerful feature that allows you to create columns that are automatically computed from other columns within the same table. These columns can simplify queries, improve performance, and enforce data consistency without the need for triggers or complex application logic. Generated columns can either be virtual (computed every time you query the table and not stored physically on disk) or stored (physically stored on disk).

When to Use Generated Columns

Generated columns are particularly useful when you need to frequently access computed data. For instance, if you have a table storing product prices and tax rates, a generated column can automatically calculate the total price including tax. This approach is more efficient than computing this value in every query or storing redundant data.

Syntax for Creating a New Table with Generated Columns

When creating a new table, you can define generated columns using the following syntax:


CREATE TABLE example_table (
    id INT AUTO_INCREMENT PRIMARY KEY,
    price DECIMAL(10,2),
    tax_rate DECIMAL(10,2),
    total_price DECIMAL(10,2) AS (price + (price * tax_rate))
);

In this example, total_price is a generated column that calculates the total price of an item including tax.

Syntax for Adding Generated Columns to an Existing Table

To add a generated column to an existing table, use the ALTER TABLE statement as follows:


ALTER TABLE example_table
ADD COLUMN total_price DECIMAL(10,2) AS (price + (price * tax_rate));

This adds the total_price generated column to the example_table.

Stored vs. Virtual Generated Columns

  • Stored Generated Columns: These columns physically store the computed values on disk. This consumes additional disk space but speeds up read operations since the value is precomputed and readily available.

  • Virtual Generated Columns: These do not occupy disk space for storing the computed values. Instead, the values are computed on-the-fly whenever they are accessed. While this saves disk space, it may slightly slow down queries that access these columns frequently.

When to Use Generated Columns vs Regular Columns

Use generated columns when the column value can be derived from other columns in the table. This is especially useful for simplifying queries and ensuring data consistency. Regular columns should be used for data that cannot be derived from other columns, such as user input or values that change independently.

If your data can be simply derived from other columns, using a generated column can help you avoid redundancy and ensure that the data remains consistent.

Generated Columns vs Views

  • Generated Columns: Are tied to a specific table and are useful for storing or dynamically computing values based on the contents of that table. They are best used when you need to store or frequently access computed values.

  • Views: Are virtual tables created by querying data from one or more real tables. They do not store data themselves but present it in a specific format. Views are ideal for complex calculations or aggregations that span multiple tables or for simplifying complex queries.

Turn your SQL into Beautiful Dashboards

Create amazing KPI dashboards directly from your SQL database with Dashase

  • Create charts, tables, and widgets
  • Collaboration - Shared Dashboards
  • AI assisted query generation - GPT-4
  • Supports PostgreSQL, MySQL, SQL Server, and more

MySQL Snippets