Home > Database > Mysql Tutorial > How to have multiple virtual generated columns in a MySQL table using CREATE TABLE statement?

How to have multiple virtual generated columns in a MySQL table using CREATE TABLE statement?

王林
Release: 2023-09-10 23:49:02
forward
856 people have browsed it

如何使用 CREATE TABLE 语句在 MySQL 表中拥有多个虚拟生成列?

It is very possible to add multiple virtual generated columns in a MySQL table. This can be illustrated with the following example:

Example

mysql> Create table profit(cost int, price int, profit int AS (price-cost), price_revised int AS (price-2));
Query OK, 0 rows affected (0.73 sec)

mysql> Describe profit;
+---------------+---------+------+-----+---------+-------------------+
| Field         | Type    | Null | Key | Default | Extra             |
+---------------+---------+------+-----+---------+-------------------+
| cost          | int(11) | YES  |     | NULL    |                   |
| price         | int(11) | YES  |     | NULL    |                   |
| profit        | int(11) | YES  |     | NULL    | VIRTUAL GENERATED |
| price_revised | int(11) | YES  |     | NULL    | VIRTUAL GENERATED |
+---------------+---------+------+-----+---------+-------------------+
4 rows in set (0.00 sec)

mysql> Insert into profit(Cost, Price) values(100,110);
Query OK, 1 row affected (0.04 sec)

mysql> Insert into profit(Cost, Price) values(200,220);
Query OK, 1 row affected (0.04 sec)

mysql> Select * from profit;
+------+-------+--------+---------------+
| cost | price | profit | price_revised |
+------+-------+--------+---------------+
| 100  | 110   | 10     | 108           |
| 200  | 220   | 20     | 218           |
+------+-------+--------+---------------+
2 rows in set (0.00 sec)
Copy after login

The above is the detailed content of How to have multiple virtual generated columns in a MySQL table using CREATE TABLE statement?. 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