What are the different types of MySQL GENERATED COLUMNS?

MySQLMySQLi Database

We have two types of MYSQL generated columns as follows −

VIRTUAL GENERATED COLUMN

As the name suggests, this kind of generated column will not take any disk space. It can be generated with or without using the keyword ‘virtual’. To understand we are illustrating it in the following example −

Example

mysql> Create table triangle(SideA DOUBLE, SideB DOUBLE, SideC DOUBLE AS (SQRT(SideA * SideB + SideB * SideB)));
Query OK, 0 rows affected (0.44 sec)

mysql> Describe Triangle;
+-------+--------+------+-----+---------+-------------------+
| Field | Type   | Null | Key | Default | Extra             |
+-------+--------+------+-----+---------+-------------------+
| SideA | double | YES  |     | NULL    |                   |
| SideB | double | YES  |     | NULL    |                   |
| SideC | double | YES  |     | NULL    | VIRTUAL GENERATED |
+-------+--------+------+-----+---------+-------------------+
3 rows in set (0.00 sec)

The above description shows that the column SideC is a virtually generated column.

STORED GENERATED COLUMN

As the name suggests, this kind of generated column will take disk space. It can be generated by using the keyword ‘stored’. To understand we are illustrating it in the following example −

Example

mysql> Create table triangle_stored(SideA DOUBLE, SideB DOUBLE, SideC DOUBLE AS (SQRT(SideA * SideB + SideB * SideB)) STORED);
Query OK, 0 rows affected (0.47 sec)

mysql> Describe triangle_stored;
+-------+--------+------+-----+---------+------------------+
| Field | Type   | Null | Key | Default | Extra            |
+-------+--------+------+-----+---------+------------------+
| SideA | double | YES  |     | NULL    |                  |
| SideB | double | YES  |     | NULL    |                  |
| SideC | double | YES  |     | NULL    | STORED GENERATED |
+-------+--------+------+-----+---------+------------------+
3 rows in set (0.00 sec)

The above description shows that the column SideC is a stored generated column.

raja
Published on 22-Feb-2018 16:09:41
Advertisements