定义
MySQL虚拟列(generated-columns)是MySQL 5.7加入的新特性。怎么理解虚拟列?从名字来讲,“生成的字段”,并不是主动插入的值。
MySQL的文档,是这么解释虚拟列的:
There are two kinds of Generated Columns: virtual (default) and stored. Virtual means that the column will be calculated on the fly when a record is read from a table. Stored means that the column will be calculated when a new record is written in the table, and after that it will be treated as a regular field. Both types can have NOT NULL restrictions, but only a stored Generated Column can be be a part of an index.
解释起来,就是虚拟列支持两种方式,virtual和stored。当在表里读取记录的时候,virtual类型的会进行实时的计算。当写入一条记录的时候,stored类型会通过计算,写入表中,和常规的字段的一样的占用磁盘的空间。这两种类型都可以有NOT NULL限制,但是能使用索引的一部分的功能。
MySQL的官方,提供了一个例子,用来简单的说明虚拟列的作用。
> CREATE TABLE sales(
name VARCHAR(20),
price_eur DOUBLE,
amount INT,
tota