Createtabletriangle(SideADOUBLE,SideBDOUBLE,SideCDOUBLEAS(SQRT(SideA*SideB+SideB*SideB)));QueryOK,0rowsaffected(0.44sec)mysql>DescribeTriangle;+------+--"/> Createtabletriangle(SideADOUBLE,SideBDOUBLE,SideCDOUBLEAS(SQRT(SideA*SideB+SideB*SideB)));QueryOK,0rowsaffected(0.44sec)mysql>DescribeTriangle;+------+--">

Home >Database >Mysql Tutorial >How to use MySQL virtual generated columns with mathematical expressions?

How to use MySQL virtual generated columns with mathematical expressions?

WBOY
WBOYforward
2023-09-01 23:29:08740browse

MySQL 虚拟生成列如何与数学表达式一起使用?

This can be explained with the help of an example where we create a virtually generated column in a table called "triangle". We know that virtual generated columns can be generated with or without the keyword "virtual".

Example

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)

The above description indicates that the SideC column is a virtually generated column.

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)

The above is the detailed content of How to use MySQL virtual generated columns with mathematical expressions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete