MySQL 中儲存的 GENERATED COLUMNS 如何與數學表示式一起使用?


我們舉一個示例來說明,其中我們正在名為“triangle_stored”的表中建立一個儲存的生成列。眾所周知,可以使用關鍵字“stored”生成儲存的生成的列。

示例

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)

mysql> INSERT INTO triangle_stored(SideA, SideB) Values(1,1),(3,4),(6,8);
Query OK, 3 rows affected (0.09 sec)
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)

更新於:2020-02-21

90 次瀏覽

啟動您的 職業

透過完成課程獲得認證

開始
廣告