db2 sql

页面导航:首页 > 数据库 > DB2 > db2 sql

db2 sql

来源: 作者: 时间:2016-01-23 11:15 【

DB2/SQL命令大全2006-12-25 13:21连接数据库: connect to [数据库名] user [操作用户名] using [密码]创建缓冲池(8K): create bufferpool ibmdefault8k IMMEDIATE SIZE 5000 PAGESIZE 8 K ;...

 

DB2/SQL命令大全2006-12-25 13:21连接:

  connect to [数据库名] user [操作用户名] using [密码]

创建缓冲池(8K):

  create bufferpool ibmdefault8k IMMEDIATE  SIZE 5000 PAGESIZE 8 K ;

创建缓冲池(16K)(OA_DIVERTASKRECORD):

  create bufferpool ibmdefault16k IMMEDIATE  SIZE 5000 PAGESIZE 16 K ;

创建缓冲池(32K)(OA_TASK):

  create bufferpool ibmdefault32k IMMEDIATE  SIZE 5000 PAGESIZE 32 K ;

创建表空间:

  CREATE TABLESPACE exoatbs IN DATABASE PARTITION GROUP IBMDEFAULTGROUP PAGESIZE 8K MANAGED BY SYSTEM USING ('/home/exoa2/exoacontainer') EXTENTSIZE 32 PREFETCHSIZE 16  BUFFERPOOL IBMDEFAULT8K  OVERHEAD 24.10 TRANSFERRATE 0.90  DROPPED TABLE RECOVERY OFF;

 

  CREATE TABLESPACE exoatbs16k  IN DATABASE PARTITION GROUP IBMDEFAULTGROUP PAGESIZE 16K MANAGED BY SYSTEM USING ('/home/exoa2/exoacontainer16k'   ) EXTENTSIZE 32  PREFETCHSIZE 16  BUFFERPOOL IBMDEFAULT16K  OVERHEAD 24.1 TRANSFERRATE 0.90  DROPPED TABLE RECOVERY OFF;

  CREATE TABLESPACE exoatbs32k  IN DATABASE PARTITION GROUP IBMDEFAULTGROUP PAGESIZE 32K MANAGED BY SYSTEM USING ('/home/exoa2/exoacontainer32k'   ) EXTENTSIZE 32  PREFETCHSIZE 16  BUFFERPOOL IBMDEFAULT32K  OVERHEAD 24.1 TRANSFERRATE 0.90  DROPPED TABLE RECOVERY OFF;

GRANT USE OF TABLESPACE exoatbs TO PUBLIC;

GRANT USE OF TABLESPACE exoatbs16k TO PUBLIC;

GRANT USE OF TABLESPACE exoatbs32k TO PUBLIC;

创建系统表空间:

  CREATE TEMPORARY TABLESPACE exoasystmp IN DATABASE PARTITION GROUP IBMTEMPGROUP PAGESIZE 8K  MANAGED BY SYSTEM USING ('/home/exoa2/exoasystmp'   ) EXTENTSIZE 32 PREFETCHSIZE 16 BUFFERPOOL IBMDEFAULT8K  OVERHEAD 24.10 TRANSFERRATE 0.90  DROPPED TABLE RECOVERY OFF;

  CREATE TEMPORARY TABLESPACE exoasystmp16k IN DATABASE PARTITION GROUP IBMTEMPGROUP PAGESIZE 16K MANAGED BY SYSTEM USING ('/home/exoa2/exoasystmp16k'  ) EXTENTSIZE 32 PREFETCHSIZE 16 BUFFERPOOL IBMDEFAULT16K OVERHEAD 24.10 TRANSFERRATE 0.90  DROPPED TABLE RECOVERY OFF;

  CREATE TEMPORARY TABLESPACE exoasystmp32k IN DATABASE PARTITION GROUP IBMTEMPGROUP PAGESIZE 32K MANAGED BY SYSTEM USING ('/home/exoa2/exoasystmp32k') EXTENTSIZE 32 PREFETCHSIZE 16 BUFFERPOOL IBMDEFAULT32K OVERHEAD 24.10 TRANSFERRATE 0.90  DROPPED TABLE RECOVERY OFF;

1. 启动实例(db2inst1):

db2start

2. 停止实例(db2inst1):

db2stop

3. 列出所有实例(db2inst1)

db2ilist

5.列出当前实例:

db2 get instance

4. 察看示例配置文件:

db2 get dbm cfg|more

5. 更新数据库管理器参数信息:

db2 update dbm cfg using para_name para_value

6. 创建数据库:

db2 create db test

7. 察看数据库配置参数信息

db2 get db cfg for test|more

8. 更新数据库参数配置信息

db2 update db cfg for test using para_name para_value

10.删除数据库:

db2 drop db test

11.连接数据库

db2 connect to test

12.列出所有表空间的详细信息。

db2 list tablespaces show detail

13.查询数据:

db2 select * from tb1

14.删除数据:

db2 delete from tb1 where id=1

15.创建索引:

db2 create index idx1 on tb1(id);

16.创建视图:

db2 create view view1 as select id from tb1

17.查询视图:

db2 select * from view1

18.节点编目

db2 catalog tcp node node_name remote server_ip server server_port

19.察看端口号

db2 get dbm cfg|grep SVCENAME

20.测试节点的附接

db2 attach to node_name

21.察看本地节点

db2 list node direcotry directory

22.节点反编目

db2 uncatalog node node_name

23.数据库编目

db2 catalog db db_name as db_alias at node node_name

24.察看数据库的编目

db2 list db directory

25.连接数据库

db2 connect to db_alias user user_name using user_password

26.数据库反编目

db2 uncatalog db db_alias

27.导出数据

db2 export to myfile of ixf messages msg select * from tb1

28.导入数据

db2 import from myfile of ixf messages msg replace into tb1

29.导出数据库的所有表数据

db2move test export   //远程链接的db2move test export -u username -p password

 

30.生成数据库的定义

db2look -d db_alias -a -e -m -l -x -f -o db2look.sql   

//远程链接的db2look -d db_alias -a -e -m -l -x -f -o db2look.sql  -i username -w password

31.创建数据库

db2 create db test1

32.生成定义

db2 -tvf db2look.sql

33.导入数据库所有的数据

db2move db_alias import

34.重组检查

db2 reorgchk

35.重组表tb1

db2 reorg table tb1

36.更新统计信息

db2 runstats on table tb1

37.备份数据库test

db2 backup db test

38.恢复数据库test

db2 restore db test

399\.列出容器的信息

db2 list tablespace containers for tbs_id show detail

40.创建表:

db2 ceate table tb1(id integer not null,name char(10))

41.列出所有表

db2 list tables

42.插入数据:

db2 insert into tb1 values(1,’sam’);

db2 insert into tb2 values(2,’smitty’);

. 建立数据库DB2_GCB

CREATE DATABASE DB2_GCB ON G: ALIAS DB2_GCB

USING CODESET GBK TERRITORY CN COLLATE USING SYSTEM DFT_EXTENT_SZ 32

2. 连接数据库

connect to sample1 user db2admin using 8301206

3. 建立别名

create alias db2admin.tables for sysstat.tables;

CREATE ALIAS DB2ADMIN.VIEWS FOR SYSCAT.VIEWS

create alias db2admin.columns for syscat.columns;

create alias guest.columns for syscat.columns;

4. 建立表

create table zjt_tables as

(select * from tables) definition only;

create table zjt_views as

(select * from views) definition only;

5. 插入记录

insert into zjt_tables select * from tables;

insert into zjt_views select * from views;

6. 建立视图

create view V_zjt_tables as select tabschema,tabname from zjt_tables;

7. 建立触发器

CREATE TRIGGER zjt_tables_del

AFTER DELETE ON zjt_tables

REFERENCING OLD AS O

FOR EACH ROW MODE DB2SQL

Insert into zjt_tables1 values(substr(o.tabschema,1,8),substr(o.tabname,1,10))

8. 建立唯一性索引

CREATE UNIQUE INDEX I_ztables_tabname

[size=3]ON zjt_tables(tabname);

9. 查看表

select tabname from tables

where tabname='ZJT_TABLES';

10. 查看列

select SUBSTR(COLNAME,1,20) as 列名,TYPENAME as 类型,LENGTH as 长度

from columns

where tabname='ZJT_TABLES';

11. 查看表结构

db2 describe table user1.department

db2 describe select * from user.tables

12. 查看表的索引

db2 describe indexes for table user1.department

13. 查看视图

select viewname from views

where viewname='V_ZJT_TABLES';

14. 查看索引

select indname from indexes

where indname='I_ZTABLES_TABNAME';

15. 查看存贮过程

SELECT SUBSTR(PROCSCHEMA,1,15),SUBSTR(PROCNAME,1,15)

FROM SYSCAT.PROCEDURES;

16. 类型转换(cast)

ip datatype:varchar

select cast(ip as integer)+50 from log_comm_failed

17. 重新连接

connect reset

18. 中断数据库连接

disconnect db2_gcb

19. view application

LIST APPLICATION;

20. kill application

FORCE APPLICATION(0);

db2 force applications all (强迫所有应用程序从数据库断开)

21. lock table

lock table test in exclusive mode

22. 共享

lock table test in share mode

23. 显示当前用户所有表

list tables

24. 列出所有的系统表

list tables for system

25. 显示当前活动数据库

list active databases

26. 查看命令选项

list command options

27. 系统数据库目录

LIST DATABASE DIRECTORY

28. 表空间

list tablespaces

29. 表空间容器

LIST TABLESPACE CONTAINERS FOR

Example: LIST TABLESPACE CONTAINERS FOR 1

30. 显示用户数据库的存取权限

GET AUTHORIZATIONS

31. 启动实例

DB2START

32. 停止实例

db2stop

33. 表或视图特权

grant select,delete,insert,update on tables to user

grant all on tables to user WITH GRANT OPTION

34. 程序包特权

GRANT EXECUTE

ON PACKAGE PACKAGE-name

TO PUBLIC

35. 模式特权

GRANT CREATEIN ON SCHEMA SCHEMA-name TO USER

36. 数据库特权

grant connect,createtab,dbadm on database to user

37. 索引特权

grant control on index index-name to user

38. 信息帮助(? XXXnnnnn )

例:? SQL30081

39. SQL 帮助(说明SQL 语句的语法)

help statement

例如,help SELECT

40. SQLSTATE 帮助(说明SQL 的状态和类别代码)

? sqlstate 或? class-code

41. 更改与"管理服务器"相关的口令

db2admin setid username password

42. 创建SAMPLE 数据库

db2sampl

db2sampl F:(指定安装盘)

43. 使用操作系统命令

! dir

44. 转换数据类型(cast)

SELECT EMPNO, CAST(RESUME AS VARCHAR(370))

FROM EMP_RESUME

WHERE RESUME_FORMAT = 'ascii'

45. UDF

要运行DB2 Java 存储过程或UDF,还需要更新服务器上的DB2 数据库管理程序配置,以包括在该机器上安装JDK 的路径

db2 update dbm cfg using JDK11_PATH d:sqllibjavajdk

TERMINATE

update dbm cfg using SPM_NAME sample

46. 检查DB2 数据库管理程序配置

db2 get dbm cfg

47. 检索具有特权的所有授权名

SELECT DISTINCT GRANTEE, GRANTEETYPE, 'DATABASE' FROM SYSCAT.DBAUTH

UNION

SELECT DISTINCT GRANTEE, GRANTEETYPE, 'TABLE ' FROM SYSCAT.TABAUTH

UNION

SELECT DISTINCT GRANTEE, GRANTEETYPE, 'PACKAGE ' FROM SYSCAT.PACKAGEAUTH

UNION

SELECT DISTINCT GRANTEE, GRANTEETYPE, 'INDEX ' FROM SYSCAT.INDEXAUTH

UNION

SELECT DISTINCT GRANTEE, GRANTEETYPE, 'COLUMN ' FROM SYSCAT.COLAUTH

UNION

SELECT DISTINCT GRANTEE, GRANTEETYPE, 'SCHEMA ' FROM SYSCAT.SCHEMAAUTH

UNION

SELECT DISTINCT GRANTEE, GRANTEETYPE, 'SERVER ' FROM SYSCAT.PASSTHRUAUTH

ORDER BY GRANTEE, GRANTEETYPE, 3

create table yhdab

(id varchar(10),

password varchar(10),

ywlx varchar(10),

kh varchar(10));

create table ywlbb

(ywlbbh varchar(8),

ywmc varchar(60))

48. 修改表结构

alter table yhdab ALTER kh SET DATA TYPE varchar(13);

alter table yhdab ALTER ID SET DATA TYPE varchar(13);

alter table lst_bsi alter bsi_money set data type int;

insert into yhdab values

('20000300001','123456','user01','20000300001'),

('20000300002','123456','user02','20000300002');

49. 业务类型说明

insert into ywlbb values

('user01','业务申请'),

('user02','业务撤消'),

('user03','费用查询'),

('user04','费用自缴'),

('user05','费用预存'),

('user06','密码修改'),

('user07','发票打印'),

('gl01','改用户基本信息'),

('gl02','更改支付信息'),

('gl03','日统计功能'),

('gl04','冲帐功能'),

('gl05','对帐功能'),

('gl06','计费功能'),

('gl07','综合统计')

备份数据库:

CONNECT TO EXOA;

QUIESCE DATABASE IMMEDIATE FORCE CONNECTIONS;

CONNECT RESET;

BACKUP DATABASE EXOA TO "/home/exoa2/db2bak/" WITH 2 BUFFERS BUFFER 1024 PARALLELISM 1 WITHOUT PROMPTING;

CONNECT TO EXOA;

UNQUIESCE DATABASE;

CONNECT RESET;

50. 以下是小弟在使用db2move中的一些经验,希望对大家有所帮助。

 db2   connect   to  YOURDB  

连接数据库

 db2look -d  YOURDB  -a -e -x -o creatab.sql

导出建库表的SQL

 db2move   YOURDB  export

用db2move将数据备份出来

 vi   creatab.sql

如要导入的数据库名与原数据库不同,要修改creatab.sql中CONNECT 项

如相同则不用更改

 db2move  NEWDB  load

将数据导入新库中

在导入中可能因为种种原因发生中断,会使数据库暂挂

db2    list tablespaces   show   detail

如:

      详细说明:

     装入暂挂

 总页数                          = 1652

 可用页数                        = 1652

 已用页数                         = 1652

 空闲页数                         = 不适用

 高水位标记(页)                 = 不适用

 页大小(字节)                   = 4096

 盘区大小(页)                   = 32

 预读取大小(页)                 = 32

 容器数                           = 1

 状态更改表空间标识                    = 2

 状态更改对象标识                      = 59

 db2 select tabname,tableid from syscat.tables where tableid=59

查看是哪张表挂起

表名知道后到db2move.lst(在db2move  YOURDB  export的目录中)中找到相应的.ixf文件

 db2 load from tab11.ixf of ixf terminate into db2admin.xxxxxxxxx

tab11.ixf对应的是xxxxxxxxx表

数据库会恢复正常,可再用db2 list tablespaces show detail查看

 

60.查看数据库详细参数

    get db cfg for localdb

   修改数据库参数,如 启用用户出口

    db2  update db cfg for localdb using userexit on

    当创建新数据库时,日志的缺省模式是循环日志。在这种模式下,只能实现数据库的脱机备份和恢复。

    如果要实现联机备份和恢复,必须设为归档日志模式。

    修改日志模式为:归档日志模式/循环日志模式:on/off

    db2 update db cfg for localdb using logretain on :

    注:改为on后,查看数据库配置参数logretain的值时,实际显示的是recovery。改变此参数后,

    再次连接数据库会显示数据库处于备份暂挂(BACKUP PENDING)状态。

    这时,需要做一次对数据库的脱机备份(db2 backup db ),才能使数据库状态变为正常。 

    db2 backup database to e:\dbtemp

    查看数据库哪些表是暂挂状态:(原句,不要修改语句)

    db2 Select tabname from syscat.tables where status='C'

 看那个表暂挂了就执行下面的语句解除该表的检查完整性:

    SET INTEGRITY FOR  TABLNAME ALL IMMEDIATE UNCHECKED

    SET INTEGRITY FOR  "TABLNAME1&TABLENAME2" ALL IMMEDIATE UNCHECKED

    db2 SET INTEGRITY FOR  Select tabname from syscat.tables where status='C' ALL IMMEDIATE UNCHECKED

    db2move localdb load generatedoverride no

    验证远程服务器实例配置:

    db2 list node directory

    db2 list node directory show detail

   

    查看端口是否处于LISTEN状态

    netstat -an

   

    更新数据库管理器配置文件,以便DB2 将把与服务db2icdb2 相关的端口号用于您正在使用的实例:

    db2 update database manager configuration using sename db2_db2

   

    查看许可证信息

    db2licm -l

   

   

   

   

    100. 终极导入

        注意先执行db2move dbname import 

        然后执行  db2 -tvf createdb.sql 这样就不会出现表暂挂现象

        而且后边不用再db2move dbname load了

  

    101.远程数据库编目到本地

         a.察看本地节点      db2 list node direcotry directory

         b.节点反编目         db2 uncatalog node node_name

         c.数据库编目         db2 catalog tcpip node node_name remote 172.16.121.48 server 50000

                                      db2 catalog db db_name as db_alias at node node_name

                                      db2 connect to db_name

         d.察看数据库的编目    db2 list db directory

         e.连接数据库         db2 connect to db_alias user user_name using user_password

Tags:

相关文章

    文章评论

    最 近 更 新
    热 点 排 行
    Js与CSS工具
    代码转换工具
    
    <