由于常去生产排查问题,又没有安装可视化工具,基本全靠命令,总结一下,不定时更新~
一、联接数据库
db2 connect to ***dbs
db2 connect to ***dbs user userName using Pass@word
列出所有用户表
db2 list tables
二、卸载数据(备份数据):
1、 卸载一个表中全部数据
db2 “export to 20200411.txt of del select * from table”
db2 “export to 20200411.txt of del modified by coldel | select * from table”
2、带条件卸载一个表中数据
db2 “export to 20200411.txt of del select * from table where id =‘907020000’”
db2 “export to 20200411.txt of del select * from table where status=‘01’”
db2 “export to 20200411.txt of del modified by coldel | select * from table where type =‘01’”
三、加载数据:(导入数据,回退脚本常用)
1、 以默认分隔符加载,默认为“,”号
db2 “import from 20200411.txt of del insert into table”
2、 以指定分隔符“|”加载
db2 “import from 20200411.txt of del modified by coldel| insert into table”
四、删除表中数据:
1、delete语句
db2 “delete from table”
db2 “delete from table where id=‘907020000’ or id=‘907010000’”
2、truncate (速度快,不可回滚)
db2 “TRUNCATE TABLE 表名 IMMEDIATE”
五、查询数据结构及数据:
db2 “select * from table”
db2 “select * from table where id=‘907020000’ and type=‘0001’”
db2 “select id,name,type,number from table”
六、修改表中数据:
db2 “update table set type=0 where id=‘907010000’ and status=‘02’”
db2 “update table set type=0 where id=‘02’ or id=‘03’”
七、清除数据库联接
db2 connect reset 断开数据库连接
db2 terminate 断开数据库连接
db2 force applications all 断开所有数据库连接
八、备份数据库
1、db2 backup db ***dbs
2、db2 move ***dbs export
db2 look -d ***dbs -e -x [-a] -o crttbl.sql
九、恢复数据库
1、 db2 restore db ***dbs without rolling forward
2、 db2 -tvf crtdb.sql
crtdb.sql文件内容:create db ***dbs on /db2catalog
db2 -stvf crttbl.sql
db2 move ***dbs import
十、查看数据库参数:
db2 get dbm cfg
db2 get db cfg for ***dbs
十三、修改数据库参数:
db2 update db cfg for ***dbs using LOGBUFSZ 20
db2 update db cfg for ***dbs using LOGFILSIZ 5120
改完后,应执行以下命令使其生效:
db2 stop
db2 start
补充:
1、更改表字段长度
alter table 表名 alter 字段名 set data type varchar(100)
2、新增表字段 设置默认值
alter table 表名 add column 字段名 char(1) default ‘0’
COMMENT ON COLUMN 表名.字段名 IS ‘新增表字段的描述’
3、查看当前数据库表空间分配状况
db2 list tablespaces show detail
4、重构表
①reorg table 表名
②CALL SYSPROC.ADMIN_CMD(‘reorg table 表名’)
5、查看表结构
db2 describe table
6、列出所有数据库
db2 list db directory
7、列出所有活动的数据库
db2 list active databases
8、列出当前数据库下所有的表
db2 list tables for all
db2 list tables for all | grep ABD_ (搜索ABC开头的表)
9、查看版本
# db2level
10、显示当前数据库管理实例
$ db2 get instance
11、设置实例系统启动时是否自动启动。
$ db2iauto -on 自动启动
$ db2iauto -off 不自动启动