南大通用GBase 8s的常用基础SQL语句(一)
1、创建数据库
eg1. 创建不记录日志的库testdb,参考语句如下:
CREATE DATABASE testdb; eg2. 创建带缓冲式的记录日志的数据库testdb(SQL语句不一定在事务之中,拥有者名字不被用于对象的解析),参考语句如下:
CREATE DATABASE testdb WITH BUFFERED LOG; eg3. 创建无缓冲式的记录日志的数据库testdb(SQL语句不一定在事务之中,拥有者名字不被用于对象的解析),参考语句如下:
CREATE DATABASE testdb WITH LOG; eg4. 创建ANSI的数据库(记录日志时无缓冲,SQL总在事务之中,拥有者名字被用于对象的解析),参考语句如下:
CREATE DATABASE testdb WITH LOG MODE ANSI; 2、创建普通数据表
普通数据表又被称为持久数据表,它在system catalog里注册。一个普通数据表
可对多个session和connection。创建时可以指定dbspace。
eg1、如下语句创建了一个集团信息表cti_vccinfo:
create table cti_vccinfo( vccid CHAR(6) not null, vccname VARCHAR(255), effective INTEGER default 0 not null, agentmax INTEGER default 0 not null, ivrmax INTEGER default 0 not null, updatekey VARCHAR(30), primary key (vccid) constraint PK_CTI_VI ); 3、创建临时数据表
临时数据表不在system catalog里注册。一个临时数据表只对对应的某个session或connection可见,在对应的session或connection结束时被自动清除。如果dbspace
存在的话,临时数据表将建于临时dbspace中。缺省情况下,是没有日志的。临时数据表支持索引。
eg1:如下创建一个customer_temp的表,语句如下:
CREATE TEMP TABLE customer_temp ( num SERIAL NOT NULL, name CHAR(15), create_time DATETIME YEAR TO FRACTION(3) ); eg2:也可以将正式表中customer中的数据通过select......into temp语句将数据导入到临时表中,如下实例创建了一个正式的表customer,并插入了三条数据,接着通过select....into temp语句将这个正式表中的数据导入到临时表customer_temp。
首先,创建customer普通数据表,建表语句如下:
CREATE TABLE customer ( num SERIAL NOT NULL, name CHAR(15),
create_time DATETIME YEAR TO FRACTION(3) ); 接着,在普通数据表customer中插入三条记录,语句如下:
insert into customer (name, create_time) values('amigo', '2010-11-17 15:41:00'); insert into customer (name, create_time) values('xiexingxing', '2010-11-17 15:42:00'); insert into customer (name, create_time) values('amigoxie', '2010-11-17 15:43:00'); 最后,通过select......into temp语句将普通数据表customer中的数据导入到临时表customer_temp中(注意:需要保证customer_temp表不存在,操作成功后,customer_temp中的字段为select出的字段),参考语句如下所示:
SELECT num, name, create_time FROM customer into TEMP customer_temp; 4、创建主键约束
1)主键约束定义在一个数据列或一组数据列上;
2)主键的值是不允许重复的;
3)主键的值不允许为NULL。
在2中的实例,创建了cti_vccinfo表,并指定了vccid为其主键,并将其取名为PK_CTI_VI,以方便进行删除操作。
接下来看一个使用复合主键的实例,如下语句创建了cti_humantaskgroup表,该表的serviceid和agentid组成联合主键,首先看下该表的建表语句:
create table cti_humantaskgroup ( serviceid VARCHAR(30) not null, agentid VARCHAR(30) not null, priority INTEGER default 0 not null, updatekey VARCHAR(30) ); 如下的语句为该表的serviceid和agentid创建了唯一索引:
create unique index Index_CTI_HTG on cti_humantaskgroup(
serviceid ASC, agentid ASC ); 5、创建引用约束
1)一个数据表的主键可以被同一个数据表或其它数据库表使用。主键被引用的数据表被称为父表,引用了附表的主键的数据表被称为子表;
2)如果在定义引用约束时使用了ON DELETE CASCADE,当把父表的数据行删除时,子表的相关数据行也会被自动删除。
在4中的实例中,cti_humantaskgroup表中的serviceid为cti_humantask中的主键,引用约束可在创建表的时候指明,也可以创建完成后通过alter语句创建,参考语句如下:
alter table cti_humantaskgroup add constraint foreign key (serviceid) references cti_humantask (serviceid) on delete cascade constraint FK_CTI_HTG_HT;
用户可以注意点,如上语句加上了on delete cascade,表示在删除cti_humantask表时,数据库系统会自动删除子表cti_humantaskgroup中serviceid与之相同的数据。
6、检查约束
定义了检查约束后,数据库将数据赋给一个数据列之前将根据检查约束检查数据是否满足条件。
例如创建一个student表,该表有id(学号)、name(姓名)、age(年龄)和birthday(出生日期)4个字段,age必须在5到35之间,则在创建该表时需要添加检查约束,建表语句参考如下:
create table student ( id VARCHAR(10) not null, name VARCHAR(10) not null, age INTEGER default 0 not null check (age between 5 and 35), birthday VARCHAR(8) ); 若通过如下语句插入一条不满足age的检查约束的数据:
insert into student values('1234', 'amigo', 40, '19821121'); 运行后会出现如下提示信息:
530: Check constraint (ines.c2209_13601) failed. 7、创建视图
1)创建视图时使用select语句;
2)视图在system catalog里注册;
3)视图数据不被存储在磁盘上;
4)对于一些数据表,可为不同的用户建立不同的视图;
5)可配置存取权限。
例如,创建一个student_age的视图,查出age在20~25的学生,语句如下:
CREATE VIEW student_age (id, name, age, birthday) AS SELECT id, name, age, birthday FROM student WHERE age>=20 and age<=25
若要查询视图中的数据,例如得到student_age视图中的数据,可通过select语句进行查询,参考如下:
select * from student_age; 若要删除student_age视图,语句如下:
drop view student_age;
因篇幅问题不能全部显示,请点此查看更多更全内容