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
Release: 2023-09-01 23:29:08
forward
705 people have browsed it

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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template