我们如何使用 CREATE TABLE 语句在 MySQL 表中存储多个生成列?

2023年 9月 16日 46.0k 0

我们如何使用 CREATE TABLE 语句在 MySQL 表中存储多个生成列?

很可能在 MySQL 表中添加多个存储的生成列。可以用以下示例来说明:

示例

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

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

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

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

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

登录后复制

以上就是我们如何使用 CREATE TABLE 语句在 MySQL 表中存储多个生成列?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论