8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

27.5.1 视图语法27.5.1 View Syntax

CREATE VIEW 语句用于创建一个新的视图(参见 第 15.1.23 节,“CREATE VIEW 语句”)。要修改视图的定义或删除视图,请使用 ALTER VIEW(参见 第 15.1.11 节,“ALTER VIEW 语句”),或 DROP VIEW(参见 第 15.1.35 节,“DROP VIEW 语句”)。The CREATE VIEW statement creates a new view (see Section 15.1.23, “CREATE VIEW Statement”). To alter the definition of a view or drop a view, use ALTER VIEW (see Section 15.1.11, “ALTER VIEW Statement”), or DROP VIEW (see Section 15.1.35, “DROP VIEW Statement”).

视图可以由多种类型的 SELECT 语句创建。它可以引用基表或其他视图。它可以使用连接、UNION 和子查询。SELECT 甚至不必引用任何表。以下示例定义了一个视图,从另一个表中选择两列,以及从这些列计算得出的表达式:A view can be created from many kinds of SELECT statements. It can refer to base tables or other views. It can use joins, UNION, and subqueries. The SELECT need not even refer to any tables. The following example defines a view that selects two columns from another table, as well as an expression calculated from those columns:

mysql> CREATE TABLE t (qty INT, price INT);
mysql> INSERT INTO t VALUES(3, 50), (5, 60);
mysql> CREATE VIEW v AS SELECT qty, price, qty*price AS value FROM t;
mysql> SELECT * FROM v;
+------+-------+-------+
| qty  | price | value |
+------+-------+-------+
|    3 |    50 |   150 |
|    5 |    60 |   300 |
+------+-------+-------+
mysql> SELECT * FROM v WHERE qty = 5;
+------+-------+-------+
| qty  | price | value |
+------+-------+-------+
|    5 |    60 |   300 |
+------+-------+-------+