MySQL存储的GENERATED COLUMNS如何与数学表达式一起使用?

可以借助一个示例进行说明,在该示例中,我们正在名为“ triangle_stored”的表中创建存储的生成列。众所周知,可以通过使用关键字“存储”来生成存储的生成列。

示例

mysql> Create table triangle_stored(SideA DOUBLE, SideB DOUBLE, SideC DOUBLE AS (SQRT(SideA * SideB + SideB * SideB)) STORED);

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)

mysql> INSERT INTO triangle_stored(SideA, SideB) Values(1,1),(3,4),(6,8);
Records: 3 Duplicates: 0 Warnings: 0

mysql> Select * from triangle_stored;
+-------+-------+--------------------+
| SideA | SideB | SideC              |
+-------+-------+--------------------+
|     1 |     1 | 1.4142135623730951 |
|     3 |     4 | 5.291502622129181  |
|     6 |     8 | 10.583005244258363 |
+-------+-------+--------------------+
3 rows in set (0.00 sec)
猜你喜欢