Virtual column
In relational databases a virtual column is a table column whose value is automatically computed using other columns values, or another deterministic expression. Virtual columns are defined of SQL:2003 as Generated Column, and are only implemented by some DBMSs, like MariaDB, SQL Server, Oracle, PostgreSQL, SQLite and Firebird (database server).
Implementation
There are two types of columns:- Virtual columns
- Persistent columns
IBM Db2
IBM Db2 supports Virtual column of Version 8 as Generated column.MariaDB
MariaDB is a MySQL fork. Virtual columns were added in the 5.2 tree.Expressions that can be used to compute the virtual columns have the following limitations:
- They must be deterministic
- They cannot return constant values
- They cannot use user-defined functions or stored procedures
- They cannot include other virtual columns
- They cannot make use of subqueries
Virtual columns can only be used on tables which use a storage engine which supports them. Storage engines supporting virtual columns are:
MRG_MyISAM tables can be based on MyISAM tables which include persistent columns; but the corresponding MRG_MyISAM column should be defined as a regular column.
Syntax
A CREATE TABLE or ALTER TABLE statement can be used to add a virtual column. The syntax used to define a virtual column is the following:MySQL
Support for virtual columns, known in MySQL as generated columns, started becoming available in MySQL 5.7. Various limitations on their use have been relaxed in subsequent versions.Oracle
Since version 11g, Oracle supports virtual columns.SQL Server
Microsoft SQL Server supports virtual columns, but they are called Computed Columns.SQL Server supports both persisted and non-persisted computed columns.
Firebird
Firebird has always supported virtual columns as its precursor InterBase supports it, called Computed Columns.Firebird supports virtual columns, not persistent ones and allows for sub-selects, calling built in functions, external functions and stored routines in the virtual column expression.
Syntax
Creating a virtual column can be done during table creation or when adding columns to an existing table. The syntax used to define a virtual column is the following:column_name COMPUTED BY
or the industry standard
column_name GENERATED ALWAYS AS