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

 
命令行窗口
  1. #打开命令行窗口  
  2. db2cmd  
  3. #打开控制中心  
  4. db2cmd db2cc  
  5. #打开命令编辑器  
  6. db2cmd db2ce  

操作数据库命令
  1. #启动数据库实例  
  2. db2start  
  3. #停止数据库实例  
  4. db2stop  
  5. 如果你不能停止数据库由于激活的连接,在运行db2stop前执行db2 force application all就可以了  
  6. #创建数据库  
  7. db2 create db [dbname]  
  8. #连接到数据库  
  9. db2 connect to [dbname] user [username] using [password]  
  10. #断开数据库连接  
  11. db2 connect reset  
  12. #列出所有数据库  
  13. db2 list db directory  
  14. #列出所有激活的数据库  
  15. db2 list active databases  
  16. #列出所有数据库配置  
  17. db2 get db cfg  
  18. #删除数据库  
  19. db2 drop database [dbname]  

操作数据表命令
  1. #列出所有用户表  
  2. db2 list tables  
  3. #列出所有系统表  
  4. db2 list tables for system  
  5. #列出所有表  
  6. db2 list tables for all  
  7. #列出系统表  
  8. db2 list tables for system  
  9. #列出用户表  
  10. db2 list tables for user  
  11. #列出特定用户表  
  12. db2 list tables for schema [user]  
  13.   
  14. #创建一个与数据库中某个表(t2)结构相同的新表(t1)  
  15. db2 create table t1 like t2  
  16.   
  17. #查询表  
  18. db2 "select * from table name where ..."  
  19. #显示表结构  
  20. db2 describe table tablename  
  21. #修改列  
  22. db2 alter table [tablename] alter column [columname] set data type varchar(24)  

脚本文件操作命令
  1. #执行脚本文件  
  2. db2 -tvf scripts.sql  

帮助命令
  1. #查看命令帮助  
  2. db2 ? db2start  
  3. #查看错误码信息  
  4. db2 ? 22001  

DB2   常用命令   II
  1. 1. 启动数据库    
  2.   db2start    
  3.   2. 停止数据库    
  4.   db2stop    
  5.   3. 连接数据库    
  6.   db2   connect   to   o_yd   user   db2   using   pwd    
  7.   4. 读数据库管理程序配置    
  8.   db2   get   dbm   cfg    
  9.   5. 写数据库管理程序配置    
  10.   db2   update   dbm   cfg   using   参数名   参数值    
  11.   6. 读数据库的配置    
  12.   db2   connect   to   o_yd   user   db2   using   pwd    
  13.   db2   get   db   cfg   for   o_yd    
  14.   7. 写数据库的配置    
  15.   db2   connect   to   o_yd   user   db2   using   pwd    
  16.   db2   update   db   cfg   for   o_yd   using   参数名   参数值    
  17.   8. 关闭所有应用连接    
  18.   db2   force   application   all    
  19.   db2   force   application   ID1,ID2,,,Idn   MODE   ASYNC    
  20.   (db2   list   application   for   db   o_yd   show   detail)    
  21.   9. 备份数据库    
  22.   db2   force   application   all    
  23.   db2   backup   db   o_yd   to   d:    
  24.   (db2   initialize   tape   on   \\.\tape0)    
  25.   (db2   rewind   tape   on   \\.\tape0)    
  26.   db2   backup   db   o_yd   to   \\.\tape0    
  27.   10. 恢复数据库    
  28.   db2   restore   db   o_yd   from   d:   to   d:      
  29.   db2   restore   db   o_yd   from   \\.\tape0   to   d:    
  30.   11. 绑定存储过程    
  31.   db2   connect   to   o_yd   user   db2   using   pwd    
  32.   db2   bind   c:\dfplus.bnd    
  33.   拷贝存储过程到服务器上的C:\sqllib\function目录中    
  34.   12. 整理表    
  35.   db2   connect   to   o_yd   user   db2   using   pwd    
  36.   db2   reorg   table   ydd    
  37.   db2   runstats   on   table   ydd   with   distribution   and   indexes   all    
  38.      
  39.      
  40.      
  41.      
  42.   13. 导出表数据    
  43.   db2   export   to   c:\sw.txt   of   del   select   *   from   dftz    
  44.   db2   export   to   c:\sw.ixf   of   ixf   select   *   from   dftz    
  45.   14. 导入表数据    
  46.   db2   import   from   c:\sw.txt   of   del   insert   into   ylbx.czyxx    
  47.   db2   import   to   c:\sw.txt   of   del   commitcount   5000   messages   c:\dftz.msg   insert   into   dftz    
  48.   db2   import   to   c:\dftz.ixf   of   ixf   commitcount   5000   messages   c:\dftz.msg   insert   into   dftz    
  49.   db2   import   to   c:\dftz.ixf   of   ixf   commitcount   5000   insert   into   dftz    
  50.   db2   import   to   c:\dftz.ixf   of   ixf   commitcount   5000   insert_update   into   dftz    
  51.   db2   import   to   c:\dftz.ixf   of   ixf   commitcount   5000   replace   into   dftz    
  52.   db2   import   to   c:\dftz.ixf   of   ixf   commitcount   5000   create   into   dftz       (仅IXF)    
  53.   db2   import   to   c:\dftz.ixf   of   ixf   commitcount   5000   replace_create   into   dftz     (仅IXF)    
  54.   15. 执行一个批处理文件    
  55.   db2   –tf   批处理文件名    
  56.   (文件中每一条命令用   ;结束)    
  57.   16. 自动生成批处理文件    
  58.   建文本文件:temp.sql    
  59.   select   'runstats   on   table   DB2.'   ||   tabname   ||   '   with   distribution   and   detailed   indexes   all;'   from   syscat.tables   where   tabschema='DB2'   and   type='T';    
  60.   db2   –tf   temp.sql>runstats.sql    
  61.   17. 自动生成建表(视图)语句    
  62.   在服务器上:C:\sqllib\misc目录中    
  63.   db2   connect   to   o_yd   user   db2   using   pwd    
  64.   db2look   –d   o_yd   –u   db2   –e   –p   –c   c:\o_yd.txt    
  65.      
  66.   db2look   -d   lys   -e   -a   -x   -i   db2admin   -o   c:\aa.txt    
  67.   18. 其他命令    
  68.   grant   dbadm   on   database   to   user   bb    
  69.      
  70.   19select   *   from   czyxx   fetch   first   1   rows   only    
  71.   20db2look   –d   lys   –u   db2admin   –w   –asd   –a   –e   –o   c:\mytable.txt     


db2取前十条记录
  1. db2 => select * from tableName fetch first 10 rows only   

db2修改字段长度
  1. db2 alter table db2admin.config alter cvalue set datatype varchar(255)  
  2. 例如:  
  3. db2 => alter table news alter AUTHOR set data type varchar(250)  
  4. db2 => describe table news   

db2增加字段方法
  1. alter table table_name  
  2. add column column_name datatype   
 转自:http://www.verydemo.com/demo_c161_i1549.html
原文地址:https://www.cnblogs.com/kaysily/p/3011699.html