一、语法分类
GBase 8s 数据库遵循 SQL92/99 标准。按功能可分为4类:
- 数据定义语言(DDL) 用来定义和管理数据库中的对象(如表、视图、存储过程、触发器等)。由创建(create)、修改(alter)、删除(drop)命令构成。
- 数据操纵语言(DML) 主要包括增加(insert)、删除(delete)、更新(update)三种操作。
- 数据控制语言(DCL) 用来管理用户对指定数据库对象的使用权限。包括分配(grant)和回收(revoke)。
- 数据查询语言(DQL) 用来查询数据。主要是查询(select)。
二、具体语法
(一)数据库(database)
1、创建数据库
create database [if not exists] <数据库名> [in dbs_name] [with [buffered] log | with log mode ansi] [nlscasesensitive | nlscase insensitive];
2、重命名数据库(注意:在数据库处于打开状态、正在操作或数据库不存在时候,数据库重命名会失败。)
rename database <旧数据库名> to <新数据库名>;
3、删除数据库
drop database [if exists] <数据库名>;
(二)表(table)
1、创建表
create [standard | raw] table [if not exists] <数据表名> (
colname1 data_type1,
colname2 data_type2);
2、重命名表
rename table <旧数据表名> to <新数据表名>;
3、删除表
drop table [if exists] <数据表名>;
4、增加列
alter table <数据表名 | 同义词名>
add (列名 数据类型 [before 已存在的列名]);
5、修改列
rename column <表名>.<旧列名> to <新列名>;
6、删除列
alter table <数据表名 | 同义词名> drop (列名1[, 列名2]);
(三)视图(view)
1、创建视图
create view [if not exists] <视图名> as <查询语句>;
2、删除视图
drop view if exists <视图名>;
(四)索引(index)
1、创建索引
create [unique | distinct | cluster] index [if not exists] <索引名称>
on <数据表名 | 同义词名> (<列名 | func_name(column_name)> [asc | desc]);
2、重命名索引
rename index <旧索引名> to <新索引名>;
2、删除索引
drop index [if exists] <索引名>;
(五)存储过程(procedure)
1、创建存储过程(支持重载)
create procedure [if not exists] <存储过程名>(参数1 数据类型, 参数2 数据类型, ...)
<spl code>
end procedure;
2、删除存储过程
drop procedure [if exists] <存储过程名> [(数据类型, 数据类型)];
(六)函数(function)
1、创建函数
create function [if not exists] <函数名>(param1 data_type1, param2 data_type2, ...)
returning data_type1 [as var1] [, data_type2 [as var2], ...]
<spl code>
return var1[, var2, ...];
end function;
2、删除函数
drop function [if exists] <函数名> [(data_type1, data_type2, ...)];
(七)同义词(synonym)
1、创建同义词
create [public | private] synonym [if not exists] <同义词名> for <数据表名 | 视图名 | 序列名>;
2、删除同义词
drop synonym [if exists] <同义词名>;
(八)触发器(trigger)
1、创建触发器
create [or replace] tirgger <触发器名> <insert | update [of column_name] | delete | select> on <目标表名>
<before | after | for each row>
when <条件>
<action>
2、删除触发器
drop trigger <触发器名>;
(九)插入(insert)
insert into <数据表名 | 视图名 | 同义词名> [字段1, 字段2, ...] values(值1, 值2, ...);
insert into <数据表名 | 视图名 | 同义词名> [字段1, 字段2, ...] select 字段1, 字段2, ... ;
insert into <数据表名 | 视图名 | 同义词名> [字段1, 字段2, ...] execute function <fn_name([param1, param2, ...])>;
(十)更新(update)
update <数据表名 | 视图名 | 同义词名>
set column_name1 = value1[, column_name2 = value2, ... ]
[where condition];
(十一)合并(merge)
merge into <目标表> as a
using <来源表 | 查询语句> as b
on ( a.列名1 = b.列名2 )
when matched then
update set a.col_1 = b.col_1, a.col_2 = b.col_2, ...
when not matched then
insert (a.col_1, a.col_2) values(b.col_1, b.col_2);
(十二)其他
其他语法与 Oracle 语法相同。