DB2数据库运维常用命令集锦

2014-11-24 18:40:18 · 作者: · 浏览: 0

在执行如下命令时,需要首先在客户端运行中输入db2cmd进行初始化或者su到db2的实例下操作。


一、常识性命令


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


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


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


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


db2 restore db --还原数据库


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


5、db2stop --停止数据库 db2start --启动数据库


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/db2inst2


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 --查看数据库中所有表结构


db2 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


二、导入数据:


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