Virtual column

From Wikipedia, the free encyclopedia

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 not part of any SQL standard, and are only implemented by some DBMS's, like MariaDB, SQL Server, Oracle and Firebird (database server) (COMPUTED BY syntax).

Virtual columns are not used in a normalized database[citation needed], because they cause redundancy.

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 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).

MariaDB

MariaDB is a MySQL fork. Virtual columns were added in the 5.2 tree. This feature is MariaDB specific, and is not supported by MySQL.[1]

Expressions that can be used to compute the virtual columns have the following limitations:

  • They must be deterministic.
  • They can not return constant values.
  • They can not use User Defined Functions or Stored Procedures.
  • They can not include other virtual columns.
  • They can not make use of subqueries.

Persistent columns can be indexed and can be part of a foreign key, 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:

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:

<type>  [GENERATED ALWAYS]  AS   ( <expression> )  [VIRTUAL | PERSISTENT]  [UNIQUE] [UNIQUE KEY] [COMMENT <text>]
  • 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.

Oracle

Oracle has supported virtual columns since the first release of version 11g .[2]

Oracle supports only virtual columns, not persistent ones. However, Oracle has another feature which allows the storage of the result of a query: materialized views.

Syntax

To create a virtual column, either a CREATE TABLE or an ALTER TABLE statement can be used. The syntax used to define a virtual column is the following:

column_name [TYPE] [GENERATED ALWAYS] AS (expression) [VIRTUAL]

SQL Server

Microsoft SQL Server supports virtual columns, but they are called Computed Columns.[3]

SQL Server supports only one type: Persisted. It is an equivalent of persistent columns.

Notes

External links

This article is issued from Wikipedia. The text is available under the Creative Commons Attribution/Share Alike; additional terms may apply for the media files.