zzgzyyz 发表于 2016-11-14 08:51:33

DB2命令行下的一些常用命令

命令行窗口
#打开命令行窗口
db2cmd
#打开控制中心
db2cmd db2cc
#打开命令编辑器
db2cmd db2ce
操作数据库命令
#启动数据库实例
db2start
#停止数据库实例
db2stop
如果你不能停止数据库由于激活的连接,在运行db2stop前执行db2 force application all就可以了
#创建数据库
db2 create db
#连接到数据库
db2 connect to user using
#断开数据库连接
db2 connect reset
#列出所有数据库
db2 list db directory
#列出所有激活的数据库
db2 list active databases
#列出所有数据库配置
db2 get db cfg
#删除数据库
db2 drop database
操作数据表命令
#列出所有用户表
db2 list tables
#列出所有系统表
db2 list tables for system
#列出所有表
db2 list tables for all
#列出系统表
db2 list tables for system
#列出用户表
db2 list tables for user
#列出特定用户表
db2 list tables for schema
#创建一个与数据库中某个表(t2)结构相同的新表(t1)
db2 create table t1 like t2
#查询表
db2 "select * from table name where ..."
#显示表结构
db2 describe table tablename
#修改列
db2 alter table alter column set data type varchar(24)
脚本文件操作命令
#执行脚本文件
db2 -tvf scripts.sql
帮助命令
#查看命令帮助
db2 ? db2start
#查看错误码信息
db2 ? 22001
DB2   常用命令   II
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:\sw.txt   of   del   select   *   from   dftz
db2   export   to   c:\sw.ixf   of   ixf   select   *   from   dftz
14. 导入表数据
db2   import   from   c:\sw.txt   of   del   insert   into   ylbx.czyxx
db2   import   to   c:\sw.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
db2look   -d   lys   -e   -a   -x   -i   db2admin   -o   c:\aa.txt
18. 其他命令
grant   dbadm   on   database   to   user   bb
19select   *   from   czyxx   fetch   first   1   rows   only
20db2look   –d   lys   –u   db2admin   –w   –asd   –a   –e   –o   c:\mytable.txt   

db2取前十条记录
db2 => select * from tableName fetch first 10 rows only
db2修改字段长度
db2 alter table db2admin.config alter cvalue set datatype varchar(255)
例如:
db2 => alter table news alter AUTHOR set data type varchar(250)
db2 => describe table news
db2增加字段方法
alter table table_name
add column column_name datatype
页: [1]
查看完整版本: DB2命令行下的一些常用命令