db2中的常用命令及使用方法

一、高(重要度)

1.启动一个db 2实例使用:
  net start instanceName

2.停止一个db 2实例使用:
  net stop instanceName

3.启动配置助手:
  db2=>!db2ca

4.手工配置连接:
  db2=>catalog node/db

5.删除视图:
  db2=>drop view view_name

6.创建表:
  db2=>create table tablename

7.列出活动的数据库和连接数:
  db2=>list active databases

8.断开所有应用程序与数据库的连接:
  db2=> force application all

9.以用户 通过使用密码 与标识为 的远程实例连接:
  db2=> attach to user using

10.创建名为 的数据库:
   db2=> create database

11.显式地使数据库失效:
   db2=> deactivate database

12.根据需要,显式地以用户 和密码 与数据库 连接:
   db2=> connect to [ [user ] using ]

13.断开与当前数据库的连接:
   db2=> connect reset

14.列出数据库中的表。如果没有指定任何参数,则缺省情况是列出当前用户的表:
   db2=> list tables[for {user | all | system | schema }][show detail]

15.显示一个表或视图的列信息:
   db2=> describe table

16.确定是否需要对表进行重组。这对于对所有表自动执行 runstats 很有用:
   db2=> reorgchk on table all

17.通过重构行来消除“碎片”数据并压缩信息,对表进行重组:
   db2=> reorg table

18.将数据库数据抽取到一个平面文件中:
   db2=> export

19.通过使用 IMPORT 实用程序,将数据导入到数据库:
   db2=> import

20.返回 LOAD 实用程序的进度:
   db2=> load query table [to local-message-file][nosummary | summaryonly] [showdelta]

21.创建索引:
   db2=>create index indexname on table (collumn)

22.创建双向索引:
   db2=>create index indexname on table(collumn) allow reverse scans

23.执行数据库备份:
   db2=> backup database [to ]

24.执行数据库恢复:
   db2=> restore database [from ]

25.返回两个字符串的连接:
   db2=>concat()

26.返回字符串的大写:
   db2=>upper() 或 ucase()

27.创建触发器:
   db2=>create trigger triggername

28.列出有关表空间的基本或者详细信息:
   db2=>list tablespace[show detail]

29.为指定表空间列出有关表空间容器的基本或详细信息:
   db2=>list tablespace containers for tablespacename

30.从表 names 导出到 del 类型的文件 name.del:
   db2=>export to name.del of del select * from names

31.从DEL类型的 NAMES.DEL 文件导入到名为NAMES的空表中:
   db2=>import from names.del of del insert into names

32.将IXF类型文件 product.ixf 中的所有数据载入到表product中:
   db2=>load form product.ixf of ixf insert into product

33.在装载 sales 表的时候,查看文件 sales.msg 来监控装载过程:
   db2=>load query table sales to d: sales.m

34.将数据库db2cert拷贝到另一个服务器上的一个新数据库 newcert 中:
   db2=>db2move db2cert export

35.在新数据库newcert中重建数据库结构并把表中的数据导入新数据库:
   db2=>db2move newcert import –io replace_create

36.对特定的表进行重组:
   db2=>reorg table tablename

37.索引来对特定的表进行重组:
   db2=>reorg table tablename index indexname

38.将数据库db2cert备份到 c:dbbackup 目录:
   db2=>backup database db2cert to c:dbbackup

39.从目录 c:dbbackup 下还原数据库 db2cert:
   db2=>restore database db2cert from c:dbbackup

40.将数据库还原为一个名叫 newdb 的新数据库,并分配两个缓冲区,每个大小为 512:
   db2=>restore database db2cert from c:dbbackup
   db2=>into newdb
   db2=>with 2 buffers
   db2=>buffer 512
   db2=>without rolling forword

41.备份数据库 db2cert 的编目表空间 syscatspace 和用户表空间 filets 到 c:dbbackup 目录下:
   db2=>backup database db2cert tablespace (syscatspace,filets) to c:dbbackup

42.以联机模式从 c:dbbackup 目录还原数据库 db2cert 的表空间 filets:
   db2=>restore database db2cert tablespace (filets) online from c:dbbackup

43.前滚由 restore 命令导致处于前滚挂起状态的数据库 db2cert,前滚执行至日志的结尾:
   db2=>rollforward database db2cert to end of logs

44.创建一个一致点,使其能用于以后的前滚恢复:
   db2=>quiesce tablespace for table

45.定义游标:
   db2=> declare cursor1 cursor with hold
         (如果没有加with hold 选项,在Commit和Rollback时,该游标将被关闭)
         for select market_code from tb_market_code  for update
         (cursor1定义为可修改的游标)

46.杀掉死锁进程:
   db2=> db2 force application(handle)

47.调用存储过程:
   db2=>call  存储过程名(参数列表)

48.将存储过程导出:
   db2=>get routine into 文件名 from procedure 存储过程名

49.导入存储过程:
   db2=>put routine from 文件名

50.连接数据库:
   db2=>connect to database_name user 用户名 using 密码

51.建立别名:
   create alias db2admin.tables for sysstat.tables
   create alias db2admin.views fro syscat.views
   create alias db2admin.columns for syscat.columns
   create alias guest.columns for syscat.columns

52.插入记录:
   db2=>insert into zjt_tables select * from tables

53.建立唯一性索引:
   db2=> CREATE UNIQUE INDEX I_ztables_tabname ON zjt_tables(tabname)

54.查看表的索引:
   db2=> db2 describe indexes for table user1.department

55.重新连接:
   db2=> connect reset

56.中断数据库连接
db2=>disconnect db2_gcb

二、中(重要度)
1.返回数据库 的所有正常快照(仅适用于 V8):
  db2=> get health snapshot for all on

2.返回管理服务器的配置设置:
  db2=> get admin cfg

3.将管理服务器配置参数

更新为值 :
  db2=> update admin cfg using



4.创建表空间:
  db2=>create tablespace tablespacename

5.显示表的统计信息和规则,以及关于表的索引和相关规则的信息:
  db2=>reorgchk update statistics on table tablename

6.分析当前表的统计资料:
  db2=>reorgchk current statistics on table tablename

7.分析当前数据库中所以表的统计资料:
  db2=>reorgchk current statistics on table all

8.列举db2前滚的日志文件,下一个需要的归档文件和前滚进程开始后提交事务的时间:
  db2=>rollforward database db2cert query status]

9.报告数据库db2cert中所有恢复历史文件的信息:
  db2=>list history all for db2cert

10.修改数据库管理器的配置,存取此实例的所有数据库的应用程序中的sql信息都被捕获:
   db2=>update dbm configuration using dft_mon_stmt on

11.捕获激活开关的应用程序的sql信息:
   db2=>update monitor switches using statement on

12.显示所有连接进程:
   db2=> db2 list applications

13.显示当前连接进程的细节:
   db2=> db2 list applications show detail

14.显示所有锁的信息:
   db2=> db2 get snapshot for locks on sample

15.查看表结构:
   db2=> db2 describe table user1.department
   db2=>db2 describe select * from user.tables

16.共享:
   db2=>lock table test in share mode

17.显示当前用户所有表:
   db2=>list tables

18.列出所有的系统表:
   db2=>list tables for system

19.显示所有db 2命令的列表:
   db2=>!db2 ?

20.显示某条命令的相关信息:
   db2=>!db2 ? command

21.显示某个SQLCODE的解释信息:
   db2=>!db2 ? SQLnnnn

22.显示某个DB2错误的解释信息:
   db2=>!db2 ? DB2nnnn

23.返回数据库管理器配置设置:
   db2=>get dbm cfg

24.将数据库管理器配置参数

更新为值 :
   db2=>update dbm cfg using



25.显式地激活数据库:
   db2=> activate database

26.将数据库管理器配置参数

更新为值 :
   db2=> 1update dbm cfg using



27.返回数据库 的数据库配置设置:
   db2=> get db cfg for

28.显示表空间的标识、名称、类型、内容和状态:
   db2=> list tablespaces [show detail]

29.显示用 指定的表空间的容器信息:
   db2=> list tablespace containers for [show detail]

30.为数据库 在数据库目录中创建一项:
   db2=> catalog database

31.返回数据库目录的内容:
   db2=> list database directory [on ]

32.返回会话监控开关的状态:
   db2=> get monitor switches

33.为 设置会话监控开关的状态:
   db2=> update monitor switches using

34.复位性能监控程序值:
   db2=> reset monitor all

35.返回实例级别的性能信息:
   db2=> get snapshot for dbm

36.为数据库 在数据库级别返回所有性能信息:
   db2=> get snapshot for all on

37.返回动态 SQL 高速缓存的内容:
   db2=> get snapshot for dynamic sql on

38.收集表 的统计信息。表名必须是用 全限定的:
   db2=> runstats on table .

三、低(重要度)
1.返回实例的正常快照信息(仅适用于 V8):
  db2=> get health snapshot for dbm

2.将 SQL 过程抽取成二进制文件:
  db2=> get routine into from [specific] procedure [hide body]

3.从二进制文件部署 SQL 过程:
  db2=> put routine from [owner [use registers]]

4.为表和索引收集并更新统计信息:
  db2=>runstats on table tablename

5.重新绑定数据库 db2cert 上所有的包,并在文件 lizhi.log 中记录结果:
  db2=>db2rebind db2cert/1 lizhi.log

6.删除活动日志路径中所以文件名小于 s0000100.log 的文件:
  db2=>prune logfile prior to s0000100.log

7.查看当前命令行处理器的设置:
  db2=>list command options

8.db2数据库管理器获取行级锁:
  db2=>alter table (tablename) locksize table

9.强迫db2数据库管理器获取表级琐:
  db2=>lock table (tablename) in (share/exclusive) mode

10.显示数据库管理器参数的当前值和延迟值:
   db2=>get dbm cfg show detail

11.返回 DB2INSTANCE 环境变量的值:
   db2=>get instance

12.返回关于当前连接的应用程序的信息:
   db2=>list application [show detail]

13.根据句柄号与特定应用程序断开连接:
   db2=> force application (h1 [,h2,..,hn])

14.显示数据库配置参数的当前值和延迟值(仅适用于 V8):
   db2=> get db cfg show detail

15.将数据库 的数据库配置参数

更新为值 :
   db2=> update db cfg for using



16.将表空间的状态复位成正常(normal)
   db2=> quiesce tablespaces for table reset

来自:

http://blog.sina.com.cn/s/blog_4c451e0e01012v82.html

原文地址:https://www.cnblogs.com/myblog-fanghao/p/5591708.html