Virtual Columns

You are viewing an old version of this article. View the current version here.

Syntax

<type>  [GENERATED ALWAYS]  AS   ( <expression> )  [VIRTUAL | PERSISTENT]  [UNIQUE] [UNIQUE KEY] [COMMENT <text>]

The default is PERSISTENT.

Any legal deterministic expression which can be calculated should be allowed with the following exceptions:

  • subqueries or anything that depends on data outside the row are not allowed (these are not deterministic because the underlying data can change)
  • UDF and stored functions can not be used (built-in functions can be used)

The virtual columns syntax is designed to be similar to MS SQL and Oracle.

Description

There are two types of virtual columns: PERSISTENT (stored), and VIRTUAL (generated-only).

Indexes are partially supported. Virtual columns do not support primary keys and indexes can only be based on PERSISTENT virtual columns. There is partial support for foreign keys but certain options, such as ON DELETE SET NULL are not allowed.

Triggers, stored procedures, informational commands (

CREATE
TABLE

, ALTER TABLE, DESCRIBE, SHOW CREATE TABLE, and so on...), data queries (SELECT, HANDLER), and partitioning based on virtual columns are all fully supported.

In use, Virtual Columns are used in SQL queries just as if they were "real" fields. VIRTUAL and PERSISTENT virtual columns differ in how the data is fetched:

  • Values for PERSISTENT virtual columns are stored in the database and read like "real" fields.
  • Values for VIRTUAL virtual columns are not stored in the database and are always generated/calculated on the fly.
  • Any VIRTUAL virtual columns which are not involved in the query are not calculated.

If an index is defined on a virtual column then the optimizer considers using it in the same way as indexes based on "real" columns.

Implementation Differences

Computed/Virtual columns in other DBMSs are subject to various constraints that are not present in the MariaDB implementation. These constraints are treated at length in those DBMSs documentation (for example, Microsoft SQL Server).

The virtual columns implementation in MariaDB does not enforce the following restrictions (these restrictions are present in MS SQL Server):

  • MariaDB allows server variables in virtual column expressions, including @@warning_count and others that change dynamically
  • MariaDB allows user variables in virtual column expressions
  • MariaDB allows CONVERT_TZ() with a named time zone as an argument, even though time zone names and time offsets are configurable
  • DATE_FORMAT() is allowed even though month names are language-dependent;
  • MariaDB allows CAST() to non-unicode character sets, even though character sets are configurable and differ between binaries/versions
  • MariaDB allows FLOAT expressions in virtual columns, which MS SQL server considers "imprecise" due to potential cross-platform differences in floating-point implementation and precision
  • SQL Server requires ARITHABORT mode to be set, so that division by zero returns an error, and not a NULL
  • SQL Server requires QUOTED_IDENTIFIER SQL mode to be set. In MariaDB, data inserted under different settings of ANSI_QUOTES will be processed and stored differently in a virtual column that contains quoted identifiers
  • MariaDB does not allow user-defined functions, even those flagged as DETERMINISTIC

Microsoft SQL Server enforces the above restrictions by refusing to create virtual columns, refusing to allow updates to a table containing them, and, finally, refusing to use an index over such a column if it can not be guaranteed that the virtual expression is fully deterministic.

In MariaDB, as long as the SQL mode, language, and other settings that were in effect during the CREATE TABLE remain unchanged, the virtual column expression will always be evaluated the same. If the SQL mode, language, etc... are changed later, please be aware that the virtual column expression might not be evaluated the same.

In MariaDB 5.2 you only get a warning if you try to update a virtual column. Starting from 5.3 you will get an error if you try to update it under strict mode (sql_mode = 'strict_all_tables').

Other Information

Virtual columns first appeared in MariaDB 5.2.

Virtual columns was originally developed by Andrey Zhakov. It was then modified by Sanja Byelkin and Igor Babaev at Monty Program for inclusion in MariaDB.

Information about Andrey's original virtual columns patch can be found on MySQL Forge.

Examples

Here is an example table that uses both VIRTUAL and PERSISTENT virtual columns:

MariaDB [(none)]> use test
Database changed
MariaDB [test]> create table table1 (
    -> a int not null,
    -> b varchar(32),
    -> c int as (a mod 10) virtual,
    -> d varchar(5) as (left(b,5)) persistent);
Query OK, 0 rows affected (0.06 sec)

If you describe the table, you can easily see which columns are virtual by looking in the "Extra" column:

MariaDB [test]> describe table1;
+-------+-------------+------+-----+---------+------------+
| Field | Type        | Null | Key | Default | Extra      |
+-------+-------------+------+-----+---------+------------+
| a     | int(11)     | NO   |     | NULL    |            |
| b     | varchar(32) | YES  |     | NULL    |            |
| c     | int(11)     | YES  |     | NULL    | VIRTUAL    |
| d     | varchar(5)  | YES  |     | NULL    | PERSISTENT |
+-------+-------------+------+-----+---------+------------+
4 rows in set (0.00 sec)

To find out what function(s) generate the value of the virtual column you can use "<code>SHOW CREATE TABLE</code>":

MariaDB [test]> show create table table1;

| table1 | CREATE TABLE `table1` (
  `a` int(11) NOT NULL,
  `b` varchar(32) DEFAULT NULL,
  `c` int(11) AS (a mod 10) VIRTUAL,
  `d` varchar(5) AS (left(b,5)) PERSISTENT
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |

If you try to insert non-default values into a virtual column, you will receive a warning and what you tried to insert will be ignored and the derived value inserted instead:

MariaDB [test]> warnings;
Show warnings enabled.

MariaDB [test]> insert into table1 values (1, 'some text',default,default);
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> insert into table1 values (2, 'more text',5,default);
Query OK, 1 row affected, 1 warning (0.00 sec)

Warning (Code 1645): The value specified for computed column 'c' in table 'table1' ignored.

MariaDB [test]> insert into table1 values (123, 'even more text',default,'something');
Query OK, 1 row affected, 2 warnings (0.00 sec)

Warning (Code 1645): The value specified for computed column 'd' in table 'table1' ignored.
Warning (Code 1265): Data truncated for column 'd' at row 1

MariaDB [test]> select * from table1;
+-----+----------------+------+-------+
| a   | b              | c    | d     |
+-----+----------------+------+-------+
|   1 | some text      |    1 | some  |
|   2 | more text      |    2 | more  |
| 123 | even more text |    3 | even  |
+-----+----------------+------+-------+
3 rows in set (0.00 sec)

MariaDB [test]> 

You can also user virtual columns to implement "poor mans partial index". See example at end of UNIQUE constraint.

Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.