MariaDB扩展特性--虚拟列

存在于表中的列,它们的值是根据确定的表达式或者是根据表中其他列的值自动计算的。 
虚拟列有两种,分别对应了定义虚拟列的修饰关键词: 
‘VIRTUAL’修饰词含义为该虚拟列的值会在查询的时候计算生成。 
‘PERSISTENT’修饰词含义为该虚拟列的值存储子表中。 

create table example_vc_tbl(
c1 int not null auto_increment primary key,
c2 varchar(70),
vc1 int as (length(c2)) virtual,
vc2 varchar(10) as (left(c2,10)) persistent
);

MariaDB [test]> desc example_vc_tbl;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| c1    | int(11)     | NO   | PRI | NULL    | auto_increment |
| c2    | varchar(70) | YES  |     | NULL    |                |
| vc1   | int(11)     | YES  |     | NULL    | VIRTUAL        |
| vc2   | varchar(10) | YES  |     | NULL    | PERSISTENT     |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

在执行插入操作时,虚拟列使用default关键字代替。 

MariaDB [test]> insert into example_vc_tbl(c2,vc1,vc2) values('你说你双休日不去泡妞还在电脑前做什么?',default,default);
Query OK, 1 row affected (0.01 sec)

MariaDB [test]> select * from example_vc_tbl;
+----+-----------------------------------------------------------+------+--------------------------------+
| c1 | c2                                                        | vc1  | vc2                            |
+----+-----------------------------------------------------------+------+--------------------------------+
|  1 | 你说你双休日不去泡妞还在电脑前做什么?                    |   57 | 你说你双休日不去泡妞           |
+----+-----------------------------------------------------------+------+--------------------------------+
1 row in set (0.00 sec)

 

原文地址:https://www.cnblogs.com/conanwang/p/5939218.html