Virtual Column
   HOME

TheInfoList



OR:

In relational
database In computing, a database is an organized collection of data stored and accessed electronically. Small databases can be stored on a file system, while large databases are hosted on computer clusters or cloud storage. The design of databases sp ...
s a virtual column is a table
column A column or pillar in architecture and structural engineering is a structural element that transmits, through compression, the weight of the structure above to other structural elements below. In other words, a column is a compression member. ...
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
DBMS In computing, a database is an organized collection of data stored and accessed electronically. Small databases can be stored on a file system, while large databases are hosted on computer clusters or cloud storage. The design of databases spa ...
s, like
MariaDB MariaDB is a community-developed, commercially supported fork of the MySQL relational database management system (RDBMS), intended to remain free and open-source software under the GNU General Public License. Development is led by some of the ori ...
, SQL Server,
Oracle An oracle is a person or agency considered to provide wise and insightful counsel or prophetic predictions, most notably including precognition of the future, inspired by deities. As such, it is a form of divination. Description The word '' ...
,
PostgreSQL PostgreSQL (, ), also known as Postgres, is a free and open-source relational database management system (RDBMS) emphasizing extensibility and SQL compliance. It was originally named POSTGRES, referring to its origins as a successor to the In ...
,
SQLite SQLite (, ) is a database engine written in the C programming language. It is not a standalone app; rather, it is a library that software developers embed in their apps. As such, it belongs to the family of embedded databases. It is the most ...
and
Firebird (database server) Firebird is an open-source SQL relational database management system that supports Linux, Microsoft Windows, macOS and other Unix platforms. The database forked from Borland's open source edition of InterBase in 2000 but the code has been lar ...
(COMPUTED BY syntax).


Implementation

There are two types of virtual columns: * Virtual columns; * Persistent columns. Virtual columns values are computed ''on the fly'' when needed, for example when they are returned by a SELECT statement. Persistent column values are computed when a
row Row or ROW may refer to: Exercise *Rowing, or a form of aquatic movement using oars *Row (weight-lifting), a form of weight-lifting exercise Math *Row vector, a 1 × ''n'' matrix in linear algebra. *Row (database), a single, implicitly structured ...
is inserted in a table, and they are written like all other values. They can change if other values change. Both virtual and persistent columns have advantages and disadvantages: virtual columns don't consume space on the disk, but they must be computed every time a query refers to them; persistent columns don't require any CPU time, but they consume disk space. However, sometimes a choice is not available, because some DBMS's support only one column type (or neither of them).


IBM Db2

IBM Db2 Db2 is a family of data management products, including database servers, developed by IBM. It initially supported the relational model, but was extended to support object–relational features and non-relational structures like JSON a ...
supports Virtual column of Version 8 as Generated column.


MariaDB

MariaDB MariaDB is a community-developed, commercially supported fork of the MySQL relational database management system (RDBMS), intended to remain free and open-source software under the GNU General Public License. Development is led by some of the ori ...
is a
MySQL MySQL () is an open-source relational database management system (RDBMS). Its name is a combination of "My", the name of co-founder Michael Widenius's daughter My, and "SQL", the acronym for Structured Query Language. A relational database o ...
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. Persistent columns can be indexed and can be part of a
foreign key A foreign key is a set of attributes in a table that refers to the primary key of another table. The foreign key links these two tables. Another way to put it: In the context of relational databases, a foreign key is a set of attributes subject to ...
, with a few small limitations concerning constraint enforcement. Virtual columns can only be used on tables which use a storage engine which supports them. Storage engines supporting virtual columns are: *
InnoDB InnoDB is a database engine, storage engine for the database management system MySQL and MariaDB. Since the release of MySQL 5.5.5 in 2010, it replaced MyISAM as MySQL's default table type. It provides the standard ACID-compliant Database transact ...
*
MyISAM MyISAM was the default storage engine for the MySQL relational database management system versions prior to 5.5 released in December 2009. It is based on the older ISAM code, but it has many useful extensions. Filesystem Each MyISAM table is s ...
*
Aria In music, an aria (Italian: ; plural: ''arie'' , or ''arias'' in common usage, diminutive form arietta , plural ariette, or in English simply air) is a self-contained piece for one voice, with or without instrumental or orchestral accompanime ...
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: ENERATED ALWAYS AS ( ) PERSISTENT NIQUE NIQUE KEY OMMENT * ''type'' is the column's data type. * ''expression'' is the SQL expression which returns the column's value for each row. * ''text'' is an optional column comment.


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 An oracle is a person or agency considered to provide wise and insightful counsel or prophetic predictions, most notably including precognition of the future, inspired by deities. As such, it is a form of divination. Description The word '' ...
supports virtual columns.


SQL Server

Microsoft SQL Server Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications—which ma ...
supports virtual columns, but they are called Computed Columns. SQL Server supports both persisted and non-persisted computed columns.


Firebird

Firebird Firebird and fire bird may refer to: Mythical birds * Phoenix (mythology), sacred firebird found in the mythologies of many cultures * Bennu, Egyptian firebird * Huma bird, Persian firebird * Firebird (Slavic folklore) Bird species ''Various sp ...
has always supported virtual columns as its precursor
InterBase InterBase is a relational database management system (RDBMS) currently developed and marketed by Embarcadero Technologies. InterBase is distinguished from other RDBMSs by its small footprint, close to zero administration requirements, and multi-g ...
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 and when adding columns to an existing table, the syntax used to define a virtual column is the following: column_name
ype Peace River Airport is a municipally owned airport located west of the Town of Peace River, Alberta, Canada. The airport has one runway, which is , and a terminal building, which is . Northern Air is based at the airport and provides scheduled ...
COMPUTED BY (expression)
or the industry standard column_name
ype Peace River Airport is a municipally owned airport located west of the Town of Peace River, Alberta, Canada. The airport has one runway, which is , and a terminal building, which is . Northern Air is based at the airport and provides scheduled ...
GENERATED ALWAYS AS (expression)


PostgreSQL

Since version 12,
PostgreSQL PostgreSQL (, ), also known as Postgres, is a free and open-source relational database management system (RDBMS) emphasizing extensibility and SQL compliance. It was originally named POSTGRES, referring to its origins as a successor to the In ...
supports virtual columns, known as generated columns.


SQLite

Since version 3.31.0 (2020-01-22),
SQLite SQLite (, ) is a database engine written in the C programming language. It is not a standalone app; rather, it is a library that software developers embed in their apps. As such, it belongs to the family of embedded databases. It is the most ...
supports virtual columns, known as generated columns.


Notes

{{reflist


External links


Virtual Columns
in MariaDB's documentation.
MariaDB 5.2: What would you use virtual columns for?
on OpenLife.cc
Virtual Columns in Oracle Database 11g Release 1

Computed Columns in SQL Server 2008
Database management systems