db2 基本命令整理

DB2数据库操作与维护指南
安装好数据库 进行系统环境变量的设置 可以在doc命令行上面运行
例如: D:\Program Files (x86)\IBM\SQLLIB\BIN


查看表结构
describe SELECT * FROM ACT_GE_BYTEARRAY


1、db2 connect to <数据库名> --连接到本地数据库名

db2 connect to <数据库名> user <用户名> using <密码> --连接到远端数据库

2、 db2 force application all --强迫所有应用断开数据库连接


3、db2 backup db db2name<数据库名称> --备份整个数据库数据

db2 restore db <db2name> --还原数据库

4、db2 list application --查看所有连接(需要连接到具体数据库才能查看)

5、db2start --启动数据库

db2stop --停止数据库

6、create database <数据库名> using codeset utf-8 territory CN --创建数据库使用utf-8编码

7、db2 catalog 命令

db2 catalog tcpip node <接点名称> remote <远程数据库地址> server <端口号> --把远程数据库映射到本地接点一般为50000

db2 catalog db <远程数据库名称> as <接点名称> at node PUB11 --远程数据库名称到本地接点

db2 CONNECT TO <接点名称> user <用户名> using <密码> --连接本地接点访问远程数据库

8、数据库导出

db2look -d <数据库名> -u <用户> -e -o <脚本名称>.sql --导出数据库的表结构,其中用户空间一般为db2admin/db2inst1

db2look -d <数据库名> -u <用户> -t <表1> <表2> -e -o <脚本名称>.sql --导出数据库中表1和表2的表结构

db2move <数据库名> export --导出数据库数据

db2move <数据库名> export -tn <表1>,<表2> --导出数据库中表和表数据

9、数据库导入

db2 -tvf <脚本名称>.sql --把上述导出的表结构导入到数据库表结构

db2move <数据库名> load -lo replace --把上述“db2move <数据库名> export “导出的数据导入到数据库中并把相同的数据替换掉


在实际使用过程中,如果用到db2自增主键,需要使用by default, 而不是always,功能是一样的,但这样在数据移植时候会很方

便!

10、db2 connect reset 或 db2 terminate --断开与数据库的连接

11、db2set db2codepage=1208 --修改页编码为1208

12、db2 describe table <表名> --查看表结构

13、db2 list tables --查看数据库中所有表结构

list tables for system --列出所有系统表

14、db2 list tablespaces --列出表空间


15、fetch first 10 rows only --列出表中前10条数据

例如:select * from <表名> fetch first 10 rows only

16、coalesce(字段名,转换后的值) --对是null的字段进行值转换

例如:select coalesce(id,1) from <表名> --对表中id如果为null转换成1

17、dayofweek(日期) --计算出日期中是周几(1是周日,2是周一.......7是周六)

dayofweek_iso --计算出日期中是周几(1是周一.......7是周日)

例如:dayofweek(date(2008-01-16)) --它会返回是4,代表星期三

dayofweek_iso(date(2008-01-16)) --它会返回是3,代表星期三

18、dayofyear(日期) --一年中的第几天,范围在1-366范围之内

注意:参数中日期的格式是YYYY-MM-DD的形式,如果不是需要进行转换,否则函数不能使用

例如:日期是20080116必须要进行转换

dayofweek(concat(concat(concat(substr(openDate,1,4),'-'),concat(substr(openDate,5,2),'-')),substr(openDate,7,2))) as week)

这样格式正确的。

19、concatt(参数1,连接值) --把参数1加上连接值组成一个新值。

  例如: concat('aa','b') --返回是aab


1.启动数据库
db2start
2.停止数据库
db2stop
3.连接数据库
db2 connect to o_yd user db2 using pwd
4.读数据库管理程序配置
db2 get dbm cfg
5.写数据库管理程序配置
db2 update dbm cfg using 参数名 参数值
6.读数据库的配置
db2 connect to o_yd user db2 using pwd
db2 get db cfg for o_yd
7.写数据库的配置
db2 connect to o_yd user db2 using pwd
db2 update db cfg for o_yd using 参数名 参数值
8.关闭所有应用连接
db2 force application all
db2 force application ID1,ID2,,,Idn MODE ASYNC
(db2 list application for db o_yd show detail)
9.备份数据库
db2 force application all
db2 backup db o_yd to d:
(db2 initialize tape on \\.\tape0)
(db2 rewind tape on \\.\tape0)
db2 backup db o_yd to \\.\tape0
10.恢复数据库
db2 restore db o_yd from d: to d:
db2 restore db o_yd from \\.\tape0 to d:
11.绑定存储过程
db2 connect to o_yd user db2 using pwd
db2 bind c:\dfplus.bnd
拷贝存储过程到服务器上的C:\sqllib\function目录中
12.整理表
db2 connect to o_yd user db2 using pwd
db2 reorg table ydd
db2 runstats on table ydd with distribution and indexes all
13.导出表数据
db2 export to c:\dftz.txt of del select * from dftz
db2 export to c:\dftz.ixf of ixf select * from dftz
14.导入表数据
import from c:\123.txt of del insert into ylbx.czyxx
db2 import to c:\dftz.txt of del commitcount 5000 messages c:\dftz.msg insert into dftz
db2 import to c:\dftz.ixf of ixf commitcount 5000 messages c:\dftz.msg insert into dftz
db2 import to c:\dftz.ixf of ixf commitcount 5000 insert into dftz
db2 import to c:\dftz.ixf of ixf commitcount 5000 insert_update into dftz
db2 import to c:\dftz.ixf of ixf commitcount 5000 replace into dftz
db2 import to c:\dftz.ixf of ixf commitcount 5000 create into dftz (仅IXF)
db2 import to c:\dftz.ixf of ixf commitcount 5000 replace_create into dftz (仅IXF)

15.执行一个批处理文件
db2 –tf 批处理文件名
(文件中每一条命令用 ;结束)
16.自动生成批处理文件
建文本文件:temp.sql
select 'runstats on table DB2.' || tabname || ' with distribution and detailed indexes all;' from syscat.tables where
tabschema='DB2' and type='T';
db2 –tf temp.sql>runstats.sql
17.自动生成建表(视图)语句
在服务器上:C:\sqllib\misc目录中
db2 connect to o_yd user db2 using pwd
db2look –d o_yd –u db2 –e –p –c c:\o_yd.txt
18.其他命令
grant dbadm on database to user bb
19
select * from czyxx fetch first 1 rows only
20
db2look –d ylbx –u db2admin –w –asd –a –e –o a.txt
21. 显示当前用户所有表
 list tables
22. 列出所有的系统表
 list tables for system
23. 查看表结构
db2 describe select * from user.tables
24. 查看死锁明细
db2 get snapshot for locks on yourdatdabasename
用df -k命令看看是否有些文件系统没有空间.
yumcn.com (中级程序员) 2009-09-28
工作一个多月了,因为公司要用DB2数据库,所以总是努力去看这方面的书,一段时间来有点体会也总结了一些常用的DB2命令,发出来给大家分享吧!希望对大家会有所帮忙,呵呵。。

启动DB2服务:db2start
关闭DB2服务: db2stop


一、加载数据:
1、 以默认分隔符加载,默认为“,”号
db2 "import from btpoper.txt of del insert into btpoper"
2、 以指定分隔符“|”加载
db2 "import from btpoper.txt of del modified by coldel| insert into btpoper"

二、卸载数据:
1、 卸载一个表中全部数据
db2 "export to btpoper.txt of del select * from btpoper"
db2 "export to btpoper.txt of del modified by coldel| select * from btpoper"

2、 带条件卸载一个表中数据
db2 "export to btpoper.txt of del select * from btpoper where brhid='907020000'"
db2 "export to cmmcode.txt of del select * from cmmcode where codtp='01'"
db2 "export to cmmcode.txt of del modified by coldel| select * from cmmcode where codtp='01'"

三、查询数据结构及数据:
db2 "select * from btpoper"
db2 "select * from btpoper where brhid='907020000' and oprid='0001'"
db2 "select oprid,oprnm,brhid,passwd from btpoper"

四、删除表中数据:
db2 "delete from btpoper"
db2 "delete from btpoper where brhid='907020000' or brhid='907010000'"

五、修改表中数据:
db2 "update svmmst set prtlines=0 where brhid='907010000' and jobtp='02'"
db2 "update svmmst set prtlines=0 where jobtp='02' or jobtp='03'"

六、联接数据库
db2 connect to btpdbs

七、清除数据库联接
db2 connect reset 断开数据库连接
db2 terminate 断开数据库连接
db2 force applications all 断开所有数据库连接

八、备份数据库
1、 db2 backup db btpdbs
2、 db2move btpdbs export
db2look -d btpdbs -e -x [-a] -o crttbl.sql

九、恢复数据库
1、 db2 restore db btpdbs without rolling forward
2、 db2 -tvf crtdb.sql
crtdb.sql文件内容:create db btpdbs on /db2catalog
db2 -stvf crttbl.sql
db2move btpdbs import

十、DB2帮助命令:
db2 ?
db2 ? restroe
db2 ? sqlcode (例:db2 ? sql0803) 注:code必须为4位数,不够4位,前面补0


十一、bind命令:将应用程序与数据库作一捆绑,每次恢复数据库后,建议都要做一次bind
(1) db2 bind br8200.bnd
(2) /btp/bin/bndall /btp/bnd
/btp/bin/bndall /btp/tran/bnd

十二、查看数据库参数:
db2 get dbm cfg
db2 get db cfg for btpdbs

十三、修改数据库参数:
db2 update db cfg for btpdbs using LOGBUFSZ 20
db2 update db cfg for btpdbs using LOGFILSIZ 5120
改完后,应执行以下命令使其生效:
db2 stop
db2 start


db2 set schema btp 修改当前模式为"btp"

db2 list tablespaces show detail 查看当前数据库表空间分配状况
db2 list tablespace containers for 2 show detail 查看tablespace id=2使用容器所在目录
db2 list application
db2 list db directory 列出所有数据库
db2 list active databases 列出所有活动的数据库
db2 list tables for all 列出当前数据库下所有的表
db2 list tables for schema btp 列出当前数据库中schema为btp的表
db2 list tablespaces show detail 显示数据库空间使用情况
db2 list packages for all


db2 "import from tab76.ixf of ixf commitcount 5000 insert into achact"

db2 "create table achact_t like achact"

db2 "rename table achact_t to achact"

db2 "insert into achact_t select * from achact where txndt>=(select lstpgdt from
acmact where actno=achact.actno)"



db2 get snapshot for dynaimic sql on jining

删除一个实例:
# cd /usr/lpp/db2_07_01/instance
# ./db2idrop InstName

列出所有DB2实例:
# cd /usr/lpp/db2_07_01/bin
# ./db2ilist

为数据库建立编目
$ db2 catalog db btpdbs on /db2catalog
取消已编目的数据库btpdbs
$ db2 uncatalog db btpdbs

查看版本
# db2level

显示当前数据库管理实例
$ db2 get instance

设置实例系统启动时是否自动启动。
$ db2iauto -on 自动启动
$ db2iauto -off 不自动启动

数据库优化命令:
reorg、runstats
当数据库经过一段时间使用,数据空间会变得越来越庞大。一些delete掉
的数据仍存放在数据库中,占用数据空间,影响系统性能。因此需要定期
运行reorg、runstats命令,清除已delete的数据,优化数据结构。
db2 reorg table 表名
db2 runstats on table 表名 with distribution and indexes all
因为要优化的表比较多,所以在/btp/bin目录下提供了一个sh程序runsall,
可在当天业务结束后,运行runsall,对数据库进行优化
PART I – 概览.........................................................................................................................11 第 1章 – DB2 Express-C是什么?..........................................................................................13 1.1免费开发、部署和分发… 无限制!...............................................................................13 1.2用户帮助和技术支持.....................................................................................................14 1.3 DB2服务器..................................................................................................................14 1.4 DB2客户端和驱动........................................................................................................14 1.5 应用程序开发的自由性.................................................................................................15 1.6 DB2 版本号 DB2 版本分类........................................................................................16 1.7升级到其它的 DB2版本................................................................................................16 1.8 DB2 Express-C的维护.................................................................................................16 1.9相关免费软件................................................................................................................17 1.9.1 IBM数据工作室(Data Studio)...........................................................................17 1.9.2 DB2 Net Search Extender .....................................................................................17 1.9.3 Starter Toolkit for DB2 on Rails.............................................................................17 1.9.4 Web 2.0 Starter Toolkit for DB2 ............................................................................17 1.9.5 WebSphere Application Server – Community Edition............................................18 第 2章 – DB2相关特性和产品................................................................................................19 2.1 DB2 Express-C订购中包含的功能...............................................................................22 2.1.1 Fix packs补丁包...................................................................................................22 2.1.2高可用性灾难恢复(HADR) ................................................................................22 2.1.3数据复制(Data Replication)...............................................................................22 2.2 DB2 Express-C所不具备的功能...................................................................................23 2.2.1数据库分区............................................................................................................23 2.2.2连接集中器(Connection Concentrator ).............................................................23 2.2.3 Geodetic Extender ................................................................................................23 2.2.4工作负载管理(Workload Management, WLM) .......................................................24 2.3 DB2相关收费产品........................................................................................................24 2.3.1 DB2连接(DB2 Connect)...................................................................................24 2.3.2 WebSphere Federation Server..............................................................................24 2.3.3 WebSphere Replication Server .............................................................................25 第 3章 – 安装 DB2..................................................................................................................27 3.1安装前提条件................................................................................................................27 3.2操作系统中的安装权限.................................................................................................27 3.3安装向导.......................................................................................................................27 3.4自动安装.......................................................................................................................31 实验 #1 安装DB2 Express-C,创建 SAMPLE数据库........................................................32 第 4章 – DB2的应用环境.......................................................................................................35 实验 #2 - 创建一个新的数据库............................................................................................43 4.1 DB2配置......................................................................................................................44 4.1.1 环境变量................................................................................................................44 4.1.2 数据库管理器配置文件(dbm cfg) ...........................................................................44 4.1.3 数据库配置文件(db cfg)....................................................................................46 4.1.4 DB2 概要文件注册表.............................................................................................47 4.2 DB2管理服务器...........................................................................................................48 实验 #3 – 实例、数据库和配置管理....................................................................................49 第 5章 – DB2工具..................................................................................................................51 5.1控制中心(Control Center).........................................................................................52 5.2命令编辑器(Command Editor) .................................................................................55 5.3 SQL帮助向导(SQL Assist Wizard ).........................................................................57 5.4 显示SQL按钮..............................................................................................................58 实验 #4 使用脚本填充EXPRESS数据库...........................................................................59 5.5 脚本..............................................................................................................................60 5.5.1 SQL脚本...............................................................................................................60 5.5.2操作系统(shell)脚本..........................................................................................61 实验 #5 为EXPRESS数据库创建一个安装脚本.................................................................62 5.6任务中心(Task Center )...........................................................................................65 5.6.1工具目录(Tools Catalog)数据库........................................................................65 5.7 日志(Journal)...........................................................................................................66 5.8运行状况监视器(Health Monitor ).............................................................................67 5.8.1运行状况中心(Health Center )...........................................................................68 PART II – DB2 Express-C 数据库管......................................................................................71 第 6章 – DB2体系结构...........................................................................................................73 6.1 DB2进程模型...............................................................................................................73 6.2 DB2内存模型...............................................................................................................74 6.3 DB2存储模型...............................................................................................................75 6.3.1数据页和扩展数据块..............................................................................................75 6.3.2缓冲池...................................................................................................................76 6.3.3表空间...................................................................................................................77 第 7章 – DB2 客户端的连接....................................................................................................81 7.1 DB2 目录......................................................................................................................81 7.2 配置助手(Configuration Assistant )..........................................................................82 7.2.1服务器端的安装要求..............................................................................................82 7.2.2 Setup required at the client 客户端的安装要求......................................................84 7.2.3建立客户端服务器端概要文件............................................................................87 实验 #6 使用配置助手........................................................................................................90 第 8章 – 数据库对象...............................................................................................................93 8.1 模式..............................................................................................................................93 8.2表.................................................................................................................................93 8.2.1数据类型................................................................................................................93 8.2.2标识列...................................................................................................................96 8.2.3序列对象................................................................................................................96 8.2.4系统目录表............................................................................................................97 8.2.5已声明临时表.........................................................................................................97 实验 #7 创建一个数据表.....................................................................................................99 8.3视图............................................................................................................................101 8.4索引............................................................................................................................101 8.4.1 Design Advisor ....................................................................................................101 8.5参照完整性.................................................................................................................102 第 9章 – 数据迁移工具..........................................................................................................105 9.1 导出(EXPORT)工具...............................................................................................106 9.2 导入(IMPORT)工具................................................................................................106 9.3 使用 LOAD来导入......................................................................................................107 9.4 db2move 工具...........................................................................................................108 9.5 db2look 工具..............................................................................................................109 实验 #8 导出EXPRESS数据库的DDL............................................................................111 第 10章 – 数据库安全...........................................................................................................115 10.1 认证..........................................................................................................................116 10.2 授权..........................................................................................................................116 10.3 DBADM权限............................................................................................................118 10.4 PUBLIC 组...............................................................................................................119 10.5 GRANT和REVOKE语句 ........................................................................................119 10.6 查看授权和特权........................................................................................................119 10.7 关于组特权...............................................................................................................121 实验 #9 授予和撤销用户的权限........................................................................................122 第 11章 – 备份和恢复...........................................................................................................125 11.1 数据库的日志记录....................................................................................................125 11.2 日志的类型...............................................................................................................126 11.3 日志记录的类型.......................................................................................................126 11.3.1 循环日志记录 ....................................................................................................126 11.3.2 档案日志记录和日志保留...................................................................................127 11.4 从控制中心进行数据库日志记录...............................................................................127 11.5 日志记录的参数........................................................................................................129 11.6 数据库备份...............................................................................................................129 实验 #10 – 安排一个备份计划..........................................................................................131 11.7 数据库恢复...............................................................................................................133 11.7.1 恢复类型............................................................................................................133 11.7.2 数据库恢复........................................................................................................133 11.8 其他关于备份和恢复的操作......................................................................................134 第 12章 – 维护任务...............................................................................................................135 12.1 重组(REORG)、运行统计(RUNSTATS)、重绑定(REBIND).......................135 12.1.1重组(REORG)命令........................................................................................135 12.1.2运行统计(RUNSTATS)命令 ..........................................................................136 12.1.3 绑定/重新绑定.................................................................................................136 12.1.4 在控制中心执行维护工作...................................................................................137 12.2 维护方式...................................................................................................................139 实验#11 – 配置自动维护.................................................................................................141 第 13章 – 并行锁定...........................................................................................................143 13.1 事务(Transactions)..............................................................................................143 13.2 并行(Concurrency) ..............................................................................................143 13.3无并行控制导致的问题 .............................................................................................144 13.3.1丢失更新(Lost update)..................................................................................145 13.3.2未落实的读(Uncommitted read)....................................................................145 13.3.3不可重复读(Non-repeatable read).................................................................146 13.3.4幻象(Phantom read)......................................................................................146 13.4隔离级别(Isolation Levels)...................................................................................147 13.4.1未落实的读........................................................................................................147 13.4.2游标稳定性........................................................................................................147 13.4.3读稳定性............................................................................................................148 13.4.4可重复读............................................................................................................148 13.4.5隔离级别对比.....................................................................................................148 13.4.6设定隔离级别.....................................................................................................149 13.5锁定升级...................................................................................................................150 13.6锁定监视...................................................................................................................151 13.7锁定等待...................................................................................................................151 13.8死锁的引发侦测.....................................................................................................152 13.9并行锁定的最佳实践:..........................................................................................153 PART III – DB2 Express-C应用程序开发............................................................................155 第 14章 –SQL PL 存储过程..................................................................................................157 14.1 IBM数据工作室(Data Studio)..............................................................................158 14.1.2在Data Studio中创建一个存储过程..................................................................159 14.2 SQL PL 存储过程基础..............................................................................................161 14.2.1存储过程的结构.................................................................................................161 14.2.2 可选的存储过程属性..........................................................................................162 14.2.3参数...................................................................................................................162 14.2.4 SQL PL存储过程中的注释................................................................................163 14.2.5 复合语句............................................................................................................163 14.2.6 变量声明............................................................................................................163 14.2.7 赋值语句............................................................................................................164 14.3 游标..........................................................................................................................164 14.4 流控制......................................................................................................................164 14.5 调用存储过程 ...........................................................................................................165 14.6 错误和情况处理器....................................................................................................166 14.7 动态SQL..................................................................................................................168 第 15章 – 直接插入 SQL 过程语言、触发器、用户定义函数(UDF)..................................169 15.1直接插入SQL PL .....................................................................................................169 15.2 触发器(Trigger)....................................................................................................170 15.2.1 触发器的类型 ....................................................................................................170 实验 #12 从控制中心创建一个触发器...............................................................................174 15.3 用户定义函数 (UDF).................................................................................................177 15.3.1 标量函数(Scalar function).............................................................................177 15.3.2 表函数(Table function)..................................................................................178 实验 #13 使用IBM Data Studio创建用户定义函数(UDF)............................................179 第 16章 – DB2 pureXML......................................................................................................181 16.1 在数据库中使用XML................................................................................................181 16.2 XML数据库..............................................................................................................182 16.2.1 启用 XML的数据库............................................................................................182 16.2.2 原生 XML数据库...............................................................................................182 16.3 DB2中的XML..........................................................................................................183 16.3.1 pureXML 技术优势............................................................................................184 16.3.2 XPath基础........................................................................................................185 16.3.3 XQuery的定义..................................................................................................188 16.3.4 插入 XML文档...................................................................................................189 16.3.5 查询 XML数据...................................................................................................191 16.3.6 使用 SQL/XML执行联合操作............................................................................196 16.3.7 使用 XQuery执行联合操作................................................................................196 16.3.8 更新删除操作.................................................................................................197 16.3.9 XML 索引.........................................................................................................198 实验 #14 - SQL/XML 和 XQuery.......................................................................................200 第 17章 – 使用 Java、PHP和 Ruby进行数据库应用开发...................................................201 17.1 Java应用程序开发...................................................................................................201 17.1.1 JDBC类型 2驱动程序.......................................................................................201 17.1.2 JDBC类型 4驱动程序.......................................................................................202 17.2 PHP应用程序开发...................................................................................................203 17.2.1 DB2为PHP提供的连接选项.............................................................................203 17.2.2 Zend Core for IBM.............................................................................................204 17.3 Ruby on Rails应用程序开发.....................................................................................206 17.3.1 Startup Toolkit for DB2 on Rails ........................................................................206 附录 A — 排除故障...............................................................................................................207 A.1 查找错误代码的更多信息...........................................................................................207 A.2 SQLCODESQLSTATE .........................................................................................208 A.3 DB2 管理通知日志.....................................................................................................208 A.4 db2diag.log................................................................................................................209 A.5 CLI追踪.....................................................................................................................209 A.6 DB2缺陷补丁.........................................................................................................209 参考资源 ...............................................................................................................................210 网站..................................................................................................................................210 书籍..................................................................................................................................211
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值