首頁  >  文章  >  資料庫  >  如何使用 CREATE TABLE 語句在 MySQL 表中擁有多個虛擬產生欄位?

如何使用 CREATE TABLE 語句在 MySQL 表中擁有多個虛擬產生欄位?

王林
王林轉載
2023-09-10 23:49:02825瀏覽

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

很可能在 MySQL 表中新增多個虛擬生成列。可以用以下範例來說明:

範例

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)
#

以上是如何使用 CREATE TABLE 語句在 MySQL 表中擁有多個虛擬產生欄位?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除