DB2数据库命令简介
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.写数据库的配置
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数据库安装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:
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数据库安装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:\ of del select * from dftz
db2 export to c:\dftz.ixf of ixf select * from dftz
14.导入表数据
import from c:\ of del insert
db2 import to c:\ 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 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:\ of del select * from dftz
db2 export to c:\dftz.ixf of ixf select * from dftz
14.导入表数据
import from c:\ of del insert
db2 import to c:\ 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.自动生成建表(视图)语句
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:\
18.其他命令
grant dbadm on database to user bb
19select * from czyxx fetch first 1 rows only
20db2look -d ylbx -u db2admin -w -asd -a -e -21. 显示当前用户所有表
list tables
22.列出所有的系统表
list tables for system
23.查看表结构
db2 describe select * from user.tables
db2 connect to o_yd user db2 using pwd
db2look -d o_yd -u db2 -e -p -c c:\
18.其他命令
grant dbadm on database to user bb
19select * from czyxx fetch first 1 rows only
20db2look -d ylbx -u db2admin -w -asd -a -e -21. 显示当前用户所有表
list tables
22.列出所有的系统表
list tables for system
23.查看表结构
db2 describe select * from user.tables
一、基础篇
1、db2 connect to <数据库名> --连接到本地数据库名
1、db2 connect to <数据库名> --连接到本地数据库名
db2 connect to <数据库名> user <用户名> using <密码> --连接到远端数据库
2、 db2 force application all --强迫所有应用断开数据库连接
3、db2 backup db db2name<数据库名称> --备份整个数据库数据
db2 restore db <db2name> --还原数据库
4、db2 list application --查看所有连接(需要连接到具体数据库才能查看)
5、db2start --启动数据库
db2stop --停止数据库
6、create database <数据库名> using codeset utf-8 territory CN --创建数据库使用utf-8编码
7、db2 catalog 命令
db2 catalog tcpip node <接点名称> remote <远程数据库地址> server <端口号> --把远程数据库映射到本地接点一般为50000
db2 catalog db <远程数据库名称> as <接点名称> at node PUB11 --远程数据库名称到本地接点
db2 CONNECT TO <接点名称> user <用户名> using <密码> --连接本地接点访问远程数据库
8、数据库导出
db2look -d <数据库名> -u <用户> -e -o <脚本名称>.sql --导出数据库的表结构,其中用户空间一般为db2admin/db2inst1
db2look -d <数据库名> -u <用户> -t <表1> <表2> -e -o <脚本名称>.sql --导出数据库中表1和表2的表结构
db2move <数据库名> export --导出数据库数据
db2move <数据库名> export -tn <表1>,<表2> --导出数据库中表和表数据
9、数据库导入
db2 -tvf <脚本名称>.sql --把上述导出的表结构导入到数据库表结构
db2move <数据库名> load -lo replace --把上述“db2move <数据库名> export “导出的数据导入到数据库中并把相同的数据替换掉
在实际使用过程中,如果用到db2自增主键,需要使用by default, 而不是always,功能是一样的,但这样在数据移植时候会很方
便!
10、db2 connect reset 或 db2 terminate --断开与数据库的连接
11、db2set db2codepage=1208 --修改页编码为1208
12、db2 describe table <表名> --查看表结构
在实际使用过程中,如果用到db2自增主键,需要使用by default, 而不是always,功能是一样的,但这样在数据移植时候会很方
便!
10、db2 connect reset 或 db2 terminate --断开与数据库的连接
11、db2set db2codepage=1208 --修改页编码为1208
12、db2 describe table <表名> --查看表结构
13、db2 list tables --查看数据库中所有表结构
list tables for system --列出所有系统表
14、db2 list tablespaces --列出表空间
二、高级篇
15、fetch first 10 rows only --列出表中前10条数据
例如:select * from <表名> fetch first 10 rows only
16、coalesce(字段名,转换后的值) --对是null的字段进行值转换
list tables for system --列出所有系统表
14、db2 list tablespaces --列出表空间
二、高级篇
15、fetch first 10 rows only --列出表中前10条数据
例如:select * from <表名> fetch first 10 rows only
16、coalesce(字段名,转换后的值) --对是null的字段进行值转换
例如:select coalesce(id,1) from <表名> --对表中id如果为null转换成1
17、dayofweek(日期) --计算出日期中是周几(1是周日,2是周一.......7是周六)
dayofweek_iso --计算出日期中是周几(1是周一.......7是周日)
例如:dayofweek(date(2008-01-16)) --它会返回是4,代表星期三
dayofweek_iso(date(2008-01-16)) --它会返回是3,代表星期三
18、dayofyear(日期) --一年中的第几天,范围在1-366范围之内
注意:参数中日期的格式是YYYY-MM-DD的形式,如果不是需要进行转换,否则函数不能使用
17、dayofweek(日期) --计算出日期中是周几(1是周日,2是周一.......7是周六)
dayofweek_iso --计算出日期中是周几(1是周一.......7是周日)
例如:dayofweek(date(2008-01-16)) --它会返回是4,代表星期三
dayofweek_iso(date(2008-01-16)) --它会返回是3,代表星期三
18、dayofyear(日期) --一年中的第几天,范围在1-366范围之内
注意:参数中日期的格式是YYYY-MM-DD的形式,如果不是需要进行转换,否则函数不能使用
例如:日期是20080116必须要进行转换
dayofweek(concat(concat(concat(substr(openDate,1,4),'-'),concat(substr(openDate,5,2),'-')),substr(openDate,7,2))) as week)
这样格式正确的。
19、concatt(参数1,连接值) --把参数1加上连接值组成一个新值。
例如: concat('aa','b') --返回是aab
dayofweek(concat(concat(concat(substr(openDate,1,4),'-'),concat(substr(openDate,5,2),'-')),substr(openDate,7,2))) as week)
这样格式正确的。
19、concatt(参数1,连接值) --把参数1加上连接值组成一个新值。
例如: concat('aa','b') --返回是aab
连接数据库:
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/exo
acontainer16k' ) 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);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论