
MySQL 5.7 Virtual Columns: Syntax, Use Cases, and Indexing
MySQL 5.7 introduces a new feature called virtual/generated column. It is called generated column because the data of this column is computed based on a predefined expression or from other columns.
What is Virtual Column ?
- In general virtual columns appear to be normal table columns, but their values are derived rather than being stored on disk.
- Virtual columns are one of the top features in MySQL 5.7, they can store a value that is derived from one or several other fields in the same table in a new field.
Syntax :
Syntax for adding new virtual column,
==> Alter table table_name add column column_name generated always as column_name virtual;
Example :
Alter table contacts add column generated always as mydbops_test virtual / stored.
GENERATED ALWAYS – It indicates that the column is a generated column.
VIRTUAL – The column values are not stored, but these are evaluated when rows are read.
STORED – The column values are evaluated and stored when rows are inserted or updated.
Use Cases:
Case 1 ( using concat ):
For example we have a mydbops_lab_test table structure as below,
We need to test the full name column, so populate a few row into the contacts table.
Adding a virtual column on existing table :
Case 2 ( using difference ):
- In this example i will calculate the balance amount to the test table.So i created a new table with virtual column.
- I have inserted a few records on this ( mydbops_lab_test_1 ) table.We need to calculate the balance amount of the each person.
- The virtual columns are calculated each time data is read where as the stored column are calculated and stored physically when the data is updated.
Indexing on Virtual Columns :
- In MySQL InnoDB supports the secondary indexes on virtual columns. Other type indexes are not supported ( Full Text / GIS ).
- A secondary index may be created on one or more virtual columns or on a combination of virtual columns and regular columns or stored generated columns. Secondary indexes that include virtual columns may be defined as unique.
Example :Here I have added the index on (Balance_amount) virtual generated column.Syntax:
Advantages :
- Virtual generated columns can be used as a way to simplify and unify queries.
- A complicated condition can be defined as a generated column and referred to from multiple queries on the table to ensure that all of them use exactly the same condition.
- It can be useful for working with columns of types that cannot be indexed directly, such as JSON columns
- The column [ NOT NULL ] is not supported with MariaDB while it’s allowed in 5.7 only.
Disadvantages:
- The disadvantage of virtual columns is that values are stored twice, once as the value of the generated column and once in the index.
- If a generated column is indexed, the optimizer recognizes query expressions that match the column definition and uses indexes from the column as appropriate during query execution.
Key points to remember :
Generated column expressions follows some rules:
- Sub-queries, parameters, variables, stored functions and user-defined functions are not permitted.
- A generated column definition can refer to other generated columns, but only those occurring earlier in the table definition.
- An auto_increment column cannot be used as a base column in a generated column definition.
- A create table like the destination table preserves generated column information from the original table.
- A create table select the destination table does not preserve information about whether columns in the selected-from table are generated columns.
The Select part of the statement cannot assign values to generated columns in the destination table.
Read more expert insights on MyDBOPS Blog: Visit MyDBOPS Blog