MySQL 的虚拟 GENERATED COLUMNS 列如何与数学表达式一起工作?
我们可以借助一个例子来说明,在这个例子中,我们在名为“triangle”的表中创建了一个虚拟生成列。正如我们所知,虚拟生成列可以用“virtual”关键字生成,也可以不用该关键字生成。
示例
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)
上面的描述表明,SideC 列是一个虚拟生成列。
mysql> INSERT INTO triangle(SideA, SideB) Values(1,1),(3,4),(6,8); Query OK, 3 rows affected (0.15 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> Select * from triangle; +-------+-------+--------------------+ | SideA | SideB | SideC | +-------+-------+--------------------+ | 1 | 1 | 1.4142135623730951 | | 3 | 4 | 5.291502622129181 | | 6 | 8 | 10.583005244258363 | +-------+-------+--------------------+ 3 rows in set (0.03 sec)
广告