`
zhangzcz1999
  • 浏览: 145046 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

DB2 参考手册

阅读更多
db2sql语法参考手册
db2 提供了关连式资料库的查询语言 sql (structured query language),是一种非常口语化、既易学又易懂的语法。此一语言几乎是每个资料库系统都必须提供的,用以表示关连式的操作,包含了资料的定义(ddl)以及资料的处理(dml)。sql原来拼成sequel,这语言的原型以"系统 r"的名字在 ibm 圣荷西实验室完成,经过ibm内部及其他的许多使用性及效率测试,其结果相当令人满意,并决定在系统r 的技术基础发展出来 ibm 的产品。而且美国国家标准学会(ansi)及国际标准化组织(iso)在1987遵循一个几乎是以 ibm sql 为基础的标准关连式资料语言定义。

一、资料定义 ddl(data definition language)
资料定语言是指对资料的格式和形态下定义的语言,他是每个资料库要建立时候时首先要面对的,举凡资料分哪些表格关系、表格内的有什麽栏位主键、表格和表格之间互相参考的关系等等,都是在开始的时候所必须规划好的。

1、建表格:
create table table_name(
column1 datatype [not null] [not null primary key],
column2 datatype [not null],
...)
说明: 
datatype --是资料的格式,详见表。
nut null --可不可以允许资料有空的(尚未有资料填入)。
primary key --是本表的主键。

2、更改表格 
alter table table_name
add column column_name datatype
说明:增加一个栏位(没有删除某个栏位的语法。
alter table table_name
add primary key (column_name)
说明:更改表得的定义把某个栏位设为主键。
alter table table_name
drop primary key (column_name)
说明:把主键的定义删除。

3、建立索引 
create index index_name on table_name (column_name)
说明:对某个表格的栏位建立索引以增加查询时的速度。

4、删除 
drop table_name
drop index_name

二、的资料形态 datatypes
smallint
16 位元的整数。
interger
32 位元的整数。
decimal(p,s)
p 精确值和 s 大小的十进位整数,精确值p是指全部有几个数(digits)大小值,s是指小数
点後有几位数。如果没有特别指定,则系统会设为 p=5; s=0 。
float
32位元的实数。
double
64位元的实数。
char(n)
n 长度的字串,n不能超过 254。
varchar(n)
长度不固定且其最大长度为 n 的字串,n不能超过 4000。
graphic(n)
和 char(n) 一样,不过其单位是两个字元 double-bytes, n不能超过127。这个形态是为
了支援两个字元长度的字体,例如中文字。
vargraphic(n)
可变长度且其最大长度为 n 的双字元字串,n不能超过 2000。
date
包含了 年份、月份、日期。
time
包含了 小时、分钟、秒。
timestamp
包含了 年、月、日、时、分、秒、千分之一秒。

三、资料操作 dml (data manipulation language)
资料定义好之後接下来的就是资料的操作。资料的操作不外乎增加资料(insert)、查询资料(query)、更改资料(update) 、删除资料(delete)四种模式,以下分 别介绍他们的语法:

1、增加资料:
insert into table_name (column1,column2,...)
values ( value1,value2, ...)
说明:
1.若没有指定column 系统则会按表格内的栏位顺序填入资料。
2.栏位的资料形态和所填入的资料必须吻合。
3.table_name 也可以是景观 view_name。

insert into table_name (column1,column2,...)
select columnx,columny,... from another_table
说明:也可以经过一个子查询(subquery)把别的表格的资料填入。

2、查询资料:
基本查询
select column1,columns2,...
from table_name
说明:把table_name 的特定栏位资料全部列出来
select *
from table_name
where column1 = xxx
[and column2 > yyy] [or column3 <> zzz]
说明:
1.''''*''''表示全部的栏位都列出来。
2.where 之後是接条件式,把符合条件的资料列出来。

select column1,column2
from table_name
order by column2 [desc]
说明:order by 是指定以某个栏位做排序,[desc]是指从大到小排列,若没有指明,则是从小到大
排列

组合查询
组合查询是指所查询得资料来源并不只有单一的表格,而是联合一个以上的
表格才能够得到结果的。
select *
from table1,table2
where table1.colum1=table2.column1
说明:
1.查询两个表格中其中 column1 值相同的资料。
2.当然两个表格相互比较的栏位,其资料形态必须相同。
3.一个复杂的查询其动用到的表格可能会很多个。

整合性的查询:
select count (*)
from table_name
where column_name = xxx
说明:
查询符合条件的资料共有几笔。
select sum(column1)
from table_name
说明:
1.计算出总和,所选的栏位必须是可数的数字形态。
2.除此以外还有 avg() 是计算平均、max()、min()计算最大最小值的整合性查询。
select column1,avg(column2)
from table_name
group by column1
having avg(column2) > xxx
说明:
1.group by: 以column1 为一组计算 column2 的平均值必须和 avg、sum等整合性查询的关键字
一起使用。
2.having : 必须和 group by 一起使用作为整合性的限制。

复合性的查询
select *
from table_name1
where exists (
select *
from table_name2
where conditions )
说明:
1.where 的 conditions 可以是另外一个的 query。
2.exists 在此是指存在与否。
select *
from table_name1
where column1 in (
select column1
from table_name2
where conditions )
说明: 
1. in 後面接的是一个集合,表示column1 存在集合里面。
2. select 出来的资料形态必须符合 column1。

其他查询
select *
from table_name1
where column1 like ''''x%''''
说明:like 必须和後面的''''x%'''' 相呼应表示以 x为开头的字串。
select *
from table_name1
where column1 in (''''xxx'''',''''yyy'''',..)
说明:in 後面接的是一个集合,表示column1 存在集合里面。
select *
from table_name1
where column1 between xx and yy
说明:between 表示 column1 的值介於 xx 和 yy 之间。

3、更改资料:
update table_name
set column1=''''xxx''''
where conditoins
说明:
1.更改某个栏位设定其值为''''xxx''''。
2.conditions 是所要符合的条件、若没有 where 则整个 table 的那个栏位都会全部被更改。

4、删除资料:
delete from table_name
where conditions
说明:删除符合条件的资料。

说明:关于where条件后面如果包含有日期的比较,不同数据库有不同的表达式。具体如下:
(1)如果是access数据库,则为:where mydate>#2000-01-01#
(2)如果是oracle数据库,则为:where mydate>cast(''''2000-01-01'''' as date)
或:where mydate>to_date(''''2000-01-01'''',''''yyyy-mm-dd'''')
在delphi中写成:
thedate=''''2000-01-01'''';
query1.sql.add(''''select * from abc where mydate>cast(''''+''''''''''''''''+thedate+''''''''''''''''+'''' as date)'''');

如果比较日期时间型,则为:
where mydatetime>to_date(''''2000-01-01 10:00:01'''',''''yyyy-mm-dd hh24:mi:ss'''') 


D:\>db2 -tvf ls.sql
connect to sample

数据库连接信息

数据库服务器 = DB2/NT 8.2.4
SQL 授权标识 = RYAN
本地数据库别名 = SAMPLE


Drop Table t44
DB20000I SQL 命令成功完成。

CREATE TABLE t44 ( sID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH
1, INCREMENT BY 1, no cache) , rname CHAR ( 25 ) NOT NULL )
DB20000I SQL 命令成功完成。

Insert into t44(rname) values ('Name1')
DB20000I SQL 命令成功完成。

Insert into t44(rname) values ('Name2')
DB20000I SQL 命令成功完成。

select * from t44

SID RNAME
----------- -------------------------
1 Name1
2 Name2

2 条记录已选择。


connect reset
DB20000I SQL 命令成功完成。

connect to sample

数据库连接信息

数据库服务器 = DB2/NT 8.2.4
SQL 授权标识 = RYAN
本地数据库别名 = SAMPLE


Insert into t44(rname) values ('Name3')
DB20000I SQL 命令成功完成。

select * from t44

SID RNAME
----------- -------------------------
1 Name1
2 Name2
3 Name3

3 条记录已选择。


connect reset
DB20000I SQL 命令成功完成。


D:\>

注意了这里是no cache好像正常了,缓存的缘故,仔细看看相关定义,说得很清楚了
GENERATED ALWAYS AS--(-- generation-expression --)-----------------'

identity-options:

|--+---------------------------------------------------------+--|
| .-----------------------------------------------. |
| V (1) .-1----------------. | |
'-(-----------+-START WITH--+- numeric-constant -+---+-+--)-'
| .-1----------------. |
+-INCREMENT BY--+- numeric-constant -+-+
| .-NO MINVALUE----------------. |
+-+-MINVALUE-- numeric-constant -+-----+
| .-NO MAXVALUE----------------. |
+-+-MAXVALUE-- numeric-constant -+-----+
| .-NO CYCLE-. |
+-+-CYCLE----+-----------------------+
| .-CACHE 20----------------. |
+-+-NO CACHE----------------+--------+
| '-CACHE-- integer-constant -' |
| .-NO ORDER-. |
'-+-ORDER----+-----------------------'


NO CACHE or CACHE
Specifies whether to keep some pre-allocated values in memory for faster access. If a new value is needed for the identity column, and there are none available in the cache, then the end of the new cache block must be logged. However, when a new value is needed for the identity column, and there is an unused value in the cache, then the allocation of that identity value is faster, because no logging is necessary. This is a performance and tuning option.
NO CACHE
2 Specifies that values for the identity column are not 2 to be pre-allocated. 2 2 When this option is specified, the values of the identity column 2 are not stored in the cache. 2 In this case, every request for a new identity value results in 2 synchronous I/O to the log.

CACHE integer-constant
Specifies how many values of the identity sequence are to be pre-allocated and kept in memory. When values are generated for the identity column, pre-allocating and storing values in the cache reduces synchronous I/O to the log.
If a new value is needed for the identity column and there are no unused values available in the cache, the allocation of the value involves waiting for I/O to the log. However, when a new value is needed for the identity column and there is an unused value in the cache, the allocation of that identity value can happen more quickly by avoiding the I/O to the log.

In the event of a database deactivation, either normally or due to a system failure, all cached sequence values that have not been used in committed statements are lost ; that is, they will never be used. The value specified for the CACHE option is the maximum number of values for the identity column that could be lost in case of database deactivation. (If a database is not explicitly activated, using the ACTIVATE command or API, when the last application is disconnected from the database, an implicit deactivation occurs.)

The minimum value is 2 (SQLSTATE 42815). The default value is CACHE 20.
分享到:
评论

相关推荐

    DB2 用户手册 官方 2

    DB2 用户手册 官方 中文 PDF DB2 V9 第二部分 db2dmc90_数据移动使用程序指南和参考.pdf db2f0c90_系统监视器指南和参考.pdf db2hac90_数据恢复和高可用行指南与参考.pdf db2irc90_发行说明.pdf db2irc91_修订包1的...

    DB2官方中文参考手册1

    DB2 v10的所有官方中文参考手册,由于资源大小被限制,所以放在两个压缩包中上传

    DB2数据库SQL语法参考手册

    以上资料为DB2数据库的一些sql语句的基本语法规则,仅供大家参考。

    IBM DB2数据库的SQL语法参考手册 .rtf

    IBM DB2数据库的SQL语法参考手册 完整的

    《DB2数据库实战手册》第一部分

    1、《DB2数据库实战手册》顾名思义是一本以实战教程为主的书籍,内容涵盖了16期DB2入门...3、《DB2数据库实战手册》在撰写的过程中,结合了DB2领域众多知名专家和高手的建议,对大家学习DB2数据库有非常高的参考价值;

    DB2SQL语法参考手册

    学习DB2基础知识的好材料,是熟练掌握SQL语法的必备

    DB2_v105 官方中文参考手册(1)

    由于要求上传资源要小于60Mb,所以只好将资源分成两个压缩包。 这两个压缩包包含DB2_V105数据库所有的相关官方中文文档。 这是第一个压缩包。 这是官方最新中文文档

    DB2 中文错误参考手册

    DB2 中文错误参考手册 可以根据错误代码找到相关的错误说明及原因

    db2数据库实战手册2

    《DB2数据库实战手册》作者介绍: 彭建军,毕业于云南大学信息学院计算机系,喜欢钻研...4、《DB2数据库实战手册》在撰写的过程中,结合了DB2领域众多知名专家和高手的建议,对大家学习DB2数据库有非常高的参考价值;

    DB2数据库实战手册(Part 3)

    1、《DB2数据库实战手册》由DB2china超级版主彭...4、《DB2数据库实战手册》在撰写的过程中,结合了DB2领域众多知名专家和高手的建议,对大家学习DB2数据库有非常高的参考价值; 文件保证是好的,我自己做过解压缩。

    DB2数据库实战手册(Part 1)

    4、《DB2数据库实战手册》在撰写的过程中,结合了DB2领域众多知名专家和高手的建议,对大家学习DB2数据库有非常高的参考价值; 曾在CSDN上下载过这本书,但是下载后无法解压缩,白花了不少资源分,严重鄙视之。我这...

    db2sql语法参考手册

    db2 提供了关连式资料库的查询语言 sql (structured query language),是一种非常口语化、既易学又易懂的语法。此一语言几乎是每个资料库系统都必须提供的,用以表示关连式的操作,包含了资料的定义(ddl)...

    DB2 用户手册 官方 3

    DB2 用户手册 官方 中文 PDF DB2 V9 第三部分 db2mgc90_迁移指南.pdf db2nlsc90_本地语言支持指南和参考.pdf db2p0c90_故障诊断指南.pdf db2q0c90_新增内容.pdf db2tvc90_Visual Explan教程.pdf db2xgc90_XML指南....

    DB2数据库实战手册(完全可用)第二部分

    国内首本DB2数据库实战手册横空问世 《DB2数据库实战手册》作者介绍: ...4、《DB2数据库实战手册》在撰写的过程中,结合了DB2领域众多知名专家和高手的建议,对大家学习DB2数据库有非常高的参考价值;

    DB2数据库实战手册 第二部分

    1、《DB2数据库实战手册》由DB2china超级版主彭建军(论坛ID:pengjianjun)耗时数月而...4、《DB2数据库实战手册》在撰写的过程中,结合了DB2领域众多知名专家和高手的建议,对大家学习DB2数据库有非常高的参考价值;

    DB2数据库实战手册(完全可用)第一卷部分

    国内首本DB2数据库实战手册横空问世 《DB2数据库实战手册》作者介绍: ...4、《DB2数据库实战手册》在撰写的过程中,结合了DB2领域众多知名专家和高手的建议,对大家学习DB2数据库有非常高的参考价值;

    DB2_PureSacle安装手册

    DB2 PureScale安装手册,基于DB2 V9.8 for AIX,Linux平台可参考。

    IBM DB2数据库的SQL语法参考手册

    IBM DB2数据库的SQL语法参考手册,找了一个,居然下载不下来,看看这个!

    DB2数据库实战手册(Part 2)

    1、《DB2数据库实战手册》由DB2china超级版主彭...4、《DB2数据库实战手册》在撰写的过程中,结合了DB2领域众多知名专家和高手的建议,对大家学习DB2数据库有非常高的参考价值; 文件保证是好的,我自己做过解压缩。

Global site tag (gtag.js) - Google Analytics