//新建数据库
db2 create database <dbname> AUTOMATIC STORAGE YES ON '/dbdata' using codeset utf-8 TERRITORY cn PAGESIZE 8192 RESTRICTIVE//链接数据库
db2 connect to <dbname>//创建缓存池
db2 create bufferpool BP_DATA_32K size 209715 pagesize 32k//创建表空间
db2 "CREATE LARGE TABLESPACE TBS_DATA_32K PAGESIZE 32K MANAGED BY AUTOMATIC STORAGE AUTORESIZE YES EXTENTSIZE 32 PREFETCHSIZE 64 BUFFERPOOL BP_DATA_32K" file system caching//创建schema
db2 create schema <schemaname>//设置执行默认schema
db2 set current schema=<schemaname>//授权 授予dbadm权限
db2 "grant dbadm on database to user \<username>"//设置归档日志
db2 "update db cfg for \<dbname> using NEWLOGPATH \<path>" db2 "update db cfg for \<dbname> using LOGARCHMETH1 DISK:/\<path>" db2 "update db cfg for \<dbname> using TRACKMOD YES"//释放链接
db2 terminate(归档与循环) https://blog.csdn.net/xcl168/article/details/14135883
1.获取数据行号。
SELECT ROW_NUMBER() OVER() AS rowID FROM <tablename>2.字符串拼接
select 'R'||<columnname> from <tablename>3.去除空格
select trim(<columnname>) from <tablename>4.获取字段长度
select length(<columnname>) from <tablename>5.定位字符位置
select locate('test',<columnname>) from <tablename>6.截取字符串
select substr(<columnname>,<begindex>,<strnum>) from <tablename> //从左截取 select left(<columnname>,<strnum>) from <tablename> //从右截取 select right(<columnname>,<strnum>) from <tablename>7.时间相关 http://www.cnblogs.com/wanghonghu/archive/2012/05/25/2518604.html
8.查询获取前面几条数据
select top ? select * from <tablename> fetch frist 1 rows only9.替换字符串
select replace(<columnname>,<beforestr>,<afterstr>) from <tablename>10.case when
select case when <columnname> is null then '' else <columnname> end from <tablename>11.转义 https://www.cnblogs.com/OliverQin/p/5872876.html
1.新增、导入表
create table <tablename>( field1 varchar(100), field2 char(2), field3 timestamp ) COMPRESS YES ADAPTIVE IN "TBS_DATA_32K" INDEX IN "TBS_INDEX_16K" ORGANIZE BY ROW; PS1: 可选用压缩对表进行空间换取 [了解压缩](https://www.ibm.com/developerworks/cn/data/library/techarticle/dm-1205db210compression/)PS2: 设置按列组织表 [了解按列组织](https://www.ibm.com/developerworks/cn/data/library/techarticle/dm-1304whatsnewdb2105/) #create table by ddl db2 -tvf ./ddl/tablename.sql -td@ ps:新增函数等 均可以通过导入实现2.新增注释
COMMENT ON COLUMN "DB"."TABLENAME"."FIELD" IS "字段"3.新增主键
ALTER TABLE "DB"."TABLENAME" ADD CONSTRAINT "PK_TABLENAME" PRIMARY KEY("FIELD")4.新增索引
CREATE INDEX "DB"."IDX_TABLENAME_01" ON "DB"."TABLENAME"("FIELD" ASC) COMPRESS YES INCLUDE NULL KEYS ALLOW REVERSE SCANS; # 加入索引后 最好执行一下runstats 来使索引生效 RUNSTATS ON TABLE TABLENAME WITH DISTRIBUTION AND DETAILED INDEXES ALL PS: 了解参数 [了解更多](https://www.cnblogs.com/liufei1983/p/9671716.html)5.新增约束
ALTER TABLE "DB"."TABLENAME" ADD CONSTRAINT "U_TABLENAME" UNIQUE(FIELD1,FIELD2) PS: 建立约束时注意加入名称,否则可能建立到系统空间,导致的后果删除时报找不到该约束。6.新增字段
ALTER TABLE TABLENAME add column UPDATED_DATE_TIME TIMESTAMP NOT NULL; PS:时间戳字段在更新数据时自动更新 ALTER TABLE TABLENAME add column UPDATED_DATE_TIME TIMESTAMP GENERATED ALWAYS FOR EACH ROW ON UPDATE AS ROW CHANGE TIMESTAMP NOT NULL;7.新增字段默认值
ALTER TABLE TABLENAME ALTER COLUMN COLNAME SET DEFAULT ''8.导入数据
#load data load from ./data/tablename.dat of del modified by dumpfile=$DUMPPATH/${dt}/tablename.rej coldel0x03 codepage=1386 keeblanks nochardel usedefaults replace into tableschema.tablname NONRECOVERABLE data buffer 10000 #import data import from ./data/tablename.dat of del modified by colde10x03 codepage='1386' commitcount 10000 replace into tablename9.导入存储过程
db2 -td@ -vf ./procedures_data.sp10.创建游标
declare cur_name cursor for select col1,col2 from table1 where table1.id=111.创建视图
create view v_tablename(col1,col2) as select col1,col2 from tablename ps:视图为原表加条件结果集映射,只能用来查询1.删除数据库
drop database databasename2.删除表
drop table tablename3.删除字段
alter table tablename drop column colname PS请注意。删除字段会导致表不可用,需要reorg表。 reorg table tablename4.删除约束
alert table <tablename> drop constraint <constraintName>5.删除方法
drop specific function tableschema.functionname6.删除存储过程
drop procedure tableschema.functionname7.删除、清空表数据
#普通删除数据 delete from tablename where id=1 #快速清空表 alter table tablename not lgged initially with empty table #还可以为表导入个空文件做replace load from /dev/null of del replace into tablename8.删除索引
drop index <indexname>1.更改字段类型
#更改字段类型 alter table tablename alter column colname set data type varchar(1000)2.更新数据
update tablename set colname='111',colname1='222' where id=11.基本查询
select * from tablename where id=12.分组查询
# 普通分组 select col1,count(*) from tablename group by col1 # 分组过滤 select col1,col2 from tablename group by col1,col2 having col1='01'3.with集合查询 可用作递归
#该用法其实可以将特别复杂sql进行降维处理。使其查询逻辑更为简单。 #其中test可作为子查询,并将单个子查询简单化处理。 with test(col1,col2) as( select co1,co2 from tablename1 ),test2(c1,c2)as( select co1,co2 from tablename2 ) select col1,col2 from test,test2 where test2.c1=test.col1 [了解更多](https://blog.csdn.net/kouge94/article/details/50977801) #递归使用详见进阶使用中 一条sql实现上下级数据递归查询4.values 创建虚拟表
# 详见进阶使用 2寄存器https://www.cnblogs.com/live-and-learn077/p/5462548.html