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 --停止数据库
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条数据
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,代表星期三
例如: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
如果要把oracle表导入到db2数据库中,有些字段类型需要进行转换
具体需要看:oracle与db2数据类型转换
启动DB2服务:db2start
关闭DB2服务: db2stop
关闭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"
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 "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 "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 "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 "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 to btpdbs
七、清除数据库联接
db2 connect reset 断开数据库连接
db2 terminate 断开数据库连接
db2 force applications all 断开所有数据库连接
db2 connect reset 断开数据库连接
db2 terminate 断开数据库连接
db2 force applications all 断开所有数据库连接
八、备份数据库
db2 backup db btpdbs
db2move btpdbs export
db2look -d btpdbs -e -x [-a] -o crttbl.sql
db2 backup db btpdbs
db2move btpdbs export
db2look -d btpdbs -e -x [-a] -o crttbl.sql
九、恢复数据库
db2 restore db btpdbs without rolling forward
db2 -tvf crtdb.sql
crtdb.sql文件内容:create db btpdbs on /db2catalog
db2 -stvf crttbl.sql
db2move btpdbs import
db2 restore db btpdbs without rolling forward
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
db2 ?
db2 ? restroe
db2 ? sqlcode (例:db2 ? sql0803) 注:code必须为4位数,不够4位,前面补0
十一、bind命令:将应用程序与数据库作一捆绑,每次恢复数据库后,建议都要做一次bind
db2 bind br8200.bnd
/btp/bin/bndall /btp/bnd
/btp/bin/bndall /btp/tran/bnd
十二、查看数据库参数:
db2 get dbm cfg
db2 get db cfg for btpdbs
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 update db cfg for btpdbs using LOGBUFSZ 20
db2 update db cfg for btpdbs using LOGFILSIZ 5120
改完后,应执行以下命令使其生效:
db2 stop
db2 start