返回首页

gbase数据、南大通用产品文档:GBase8aCREATE VIEW

更新日期:2024年09月11日

功能说明
此语句用来创建一个新的视图,或者使用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 视图名;

Not enough tokens specified in %D directive representation of date string
发生此错误的原因是指定的字符串没有正确数量的标记或分隔符来构成基
于 GL_DATE %D 指令(mm/dd/yy 格式)的有效日期值。例如,12/15/98 是基
于 GL_DATE%D 指令的有效的字符串表示形式,但是 12/1598 是一个无效的字符串表示
形式,因为没有足够的分隔符或标记。
要修复此问题,请修改日期字符串表示形式,使其包含 GL_DATE%D 指令的有效格式。

在GBase ADO.NET 使用事务时需下列步骤:
1) 使用GBaseConnection 创建数据库连接对象



GBase 8a 程序员手册ADO.NET 篇
南大通用数据技术股份有限公司

- 27 -
2) 使用GBaseCommand 创建命令对象
3) 使用连接对象打开连接
4) 使用连接对象的BeginTransaction 开启事务,返回事务对象
5) 将命令对象与连接对象及事务对象关联
6) 命令对象对表进行一些操作
7) 执行事务对象的提交方法
8) 命令对象执行失败后,执行事务对象的回滚方法
下面的例子展示在GBase ADO.NET 中如何使用事务。
[ C# ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GBase.Data;
using GBase.Data.GBaseClient;
using System.Diagnostics;

namespace UsingStoredRoutines
{
class Program
{
static void Main(string[] args)
{
GBaseConnection _Conn = new GBaseConnection();
_Conn.ConnectionString =
"server=192.168.5.41;user=root;database=test;password=1;pooling=fals
e";
GBaseCommand _Cmd = new GBaseCommand();
GBaseTransaction _Trans = null;

GBase 8a 程序员手册ADO.NET 篇


- 28 -

南大通用数据技术股份有限公司

try
{
Console.WriteLine("Connecting to GBase...");
_Conn.Open();

_Trans = _Conn.BeginTransaction();
_Cmd.Connection = _Conn;
_Cmd.Transaction = _Trans;

// 在GBase 8a 集群下需显示关闭自动提交模式
// _Cmd.CommandText = "set autocommit = false";
/ _Cmd.ExecuteNonQuery();
_Cmd.CommandText
=
"insert
into
`test`(`f_varchar`)
values('111')";
_Cmd.ExecuteNonQuery();
_Cmd.CommandText
=
"insert
into
`test`(`f_varchar`)
values('222')";
_Cmd.ExecuteNonQuery();
_Trans.Commit();
Console.WriteLine("Transaction has committed.");
}
catch (GBaseException ex)
{
_Trans.Rollback();
Console.WriteLine("Error " + ex.Number + " has
occurred: " + ex.Message);
}
finally
{
if (_Conn != null)
_Conn.Close();
Console.WriteLine("None.");
}
}
}
}



GBase 8a 程序员手册ADO.NET 篇
南大通用数据技术股份有限公司

- 29 -