功能说明
此语句用来创建一个新的视图,或者使用OR REPLACE 子句来修改已存在的的
视图定义。
GBase 8a MPP Cluster 产品手册
5 数据库管理指南
文档版本953(2022-04-10)
南大通用数据技术股份有限公司
993
说明
创建视图需要有CREATE VIEW 权限,以及构成视图的SELECT
语句中引用列的部分权限。
对于在SELECT 语句中要使用的列必须
有SELECT 权限。如果使用了OR REPLACE 子句,还必须有删除
视图的权限;
在存储子程序内,定义不能引用子程序参数或局部变量;
在定义中引用的表或视图必须存在。但是,创建了视图后,能够舍
弃定义引用的表或视图。要想检查视图定义是否存在这类问题,可
使用CHECK TABLE 语句;
在视图定义中命名的表必须已存在。
不能将触发程序与视图关联在一起;
在视图定义中不能引用TEMPORARY 表;
SELECT 语句不能包含FROM 子句中的子查询;
SELECT 语句不能引用系统或用户变量;
SELECT 语句不能引用预处理语句参数;
SELECT 语句字段名后可以带注释,注释字符上限为2000 个;
创建的视图名称不能与已存在的表重名。
语法格式
CREATE [OR REPLACE] VIEW [vc_name.][database_name.]view_name [(col
umn_list)] AS select_statement;
表5- 69 参数说明
参数名称
说
明
vc_name
vc 名,可选项。
database_name
数据库名,可选项。
view_name
视图名。
column_list
视图列。
select_statement
提供给定义视图的SELECT 语句。本语句可以从其它表或者
视图中提取数据,并且支持给视图添加字段,支持给视图字
段添加注释。
select
select_expr [comment comment_value],…
from
table_references
示例
示例1:创建视图。
GBase 8a MPP Cluster 产品手册
5 数据库管理指南
文档版本953(2022-04-10)
南大通用数据技术股份有限公司
994
gbase> DROP TABLE IF EXISTS product;
Query OK, 0 rows affected
gbase> CREATE TABLE product (quantity INT,price INT);
Query OK, 0 rows affected
gbase> INSERT INTO product VALUES(3,50);
Query OK, 1 row affected
gbase> CREATE VIEW product_v AS SELECT quantity,price,quantity*price
FROM product;
Query OK, 0 rows affected
gbase> SELECT * FROM product_v;
+----------+-------+-----------------+
| quantity | price | quantity*price
|
+----------+-------+-----------------+
|
3 |
50 |
150 |
+----------+-------+-----------------+
1 row in set
创建带注释的视图:
Create view v_user as select id comment ‘user id’, addr comment ‘user office
address’ from user;
Create view v_user as select id as ‘id’ comment ‘user id’, addr as ‘addr’
comment ‘user office address’ from user;
注意
视图注释内容的长度限制与表中字段的注释长度限制相同,都是最大到
2000 字符。
查看视图字段注释的方式:
Show create view 视图名;
Show create table 视图名;