<1>查询数据库目录:
db2 list db directory
<2>查询数据库中表
db2 list tables 当前用户
db2 list tables for all 所有表
db2 list tables for schema schemaname 指定模式的表
<3>显示表结构
db2 describe table tablename
<4>插入数据
db2 insert into tablename(字段名,字段名...) values (与字段名一一对应的值)
db2 insert into tablename1(字段1,字段2,字段3...)
select 字段1,字段2,字段3...from tablename2 + 查询条件
<5>更改表或视图数据
db2 update tablename/viewname set 字段名1='',字段2='',...+查询条件
<6>删除数据
db2 delete from tablename where + 条件
<7>导入数据
db2 "import from E:\name.txt of del insert into tableName"
db2 "import from E:\name.ixf of ixf commitcount 5000 insert /create/replace into tableName"
db2 "load client from D:\xx.txt of del insert/replace into tabName"(不需要写日志,但插入前表必须存在;不能create table)
db2 "load client from D:\xx.txt of del restart/terminate into tabName" 当导入数据出现问题被强行中断时,此表会被加锁,通过此命令可以解锁
<8>导出数据
db2 "export to E:\name.txt of del select * from tableName"
db2 "export to E:\name.txt of del MODIFIED BY NOCHARDEL select * from tableName"(导出不带分号的数据)
导出表结构和数据
db2 "export to E:\name.ixf of ixf MODIFIED BY NOCHARDEL select * from tableName"
db2 "export to E:\name.ixf of ixf MODIFIED BY NOCHARDEL select * from tableName fetch first (取数+UNM) rows only"(取固定条数)
导出表结构
db2look -d dbName -e -t tableName -o D:\xxx.sql(path) -i userName -w password
db2look -d dbName -z tabSchema -e -c -i userName -w password -o + 路径名
导出存储过程结构
db2 "export to xxx.sql of del select text from syscat.procedures where procname='大写存储过程名'"
<9>查询表状态
db2 load query table + tableName
<10>查询当前表数据量(数据入库时)
db2 select count(1) from tab with ur
<11>修改当前表名、模式名
db2 rename table tab1 to tab2
2、数据定义语言(DDL:create,alter)
1 db2 create table(.....) in userspace1 INDEX in userspace2 2 3 (userspace1是表所在空间,userspace2是表上索引所在空间)
<3>创建视图
1 db2 create view viewname
2
3 as select 字段名1,字段名2...from table where + 条件
4
5 with check option 规定一种约束:通过视图插入或更新的每一行都必须符合视图的定义,如:
6
7 create view emp_view2(empno,empname,deptno) as (select id,name,dept from employee where dept=10)with check option
8
9 当此视图用于更新数据或插入新值时,with check option 限制了dept列的输入值
<4>修改表(列,主键,唯一约束,检查约束)
1 1)添加新列 alter table tablename ADD COLUMN columnname 数据类型
2
3 2)添加约束
4
5 3)修改表中字段 alter table tablename alter columnname set data type 数据类型
6
7 4) 添加主键 alter table tablename add primary key(c1,c2)
8
9 <5>删除模式、表、视图
10
11 drop schema schName <CASCADE|RESTRICT>
12
13 CASCADE(级联)表示删除模式的同时删除该模式中所有的数据库对象
14
15 RESTRICT(限制)表示该模式下定义了数据库对象时,限制删除;没有任何数据库对象时才能删除
16
17 <6>重新组织表及其索引
18
19 重组表数据 reorg table tableName index indexName(根据索引)
20
21 重组表索引 reorg indexes all for table tableName
22
23 <7>重新收集表及其索引统计信息
24
25 runstats on table tableName for indexes all(跑批前重新收集所用表信息可以提高效率)
26
27 <8>DB2自动增长主键方法
28
29 IDENTITY列
30
31 generated always as identity(start with 1,increment by 1)将一个字段指定为自增长型字段,放在数据类型后。
SEQUENCE对象(序列)
3、数据控制语言(DCL:grant,revoke)
1 将表的特权授予用户
2
3 grant select,update,delete on table tableName to user userName with grant option
4
5 将包特权授予同组
6
7 grant control on package packageName on group groupName with grant option