1.安装DB2
DB2的安装可以算是再各个数据库里面最简单的了。除了sqlite。
下载安装包,解压
[root@Enterprise tmp]# tar xvf DB2_V82_PE_LNX_32_NLV.tar
解开以后的文件都被放置在当前目录下的pe文件夹中。
开始安装:
[root@Enterprise tmp]# cd pe
[root@Enterprise pe]# ls
db2 db2_deinstall db2_install db2setup doc
[root@Enterprise pe]# ./db2_install
Specify one or more of the following keywords,
separated by spaces, to install DB2 products.
选择自己要安装的产品,输入名字后回车。等待安装完成。
我们现在需要做的是,对数据库的初始化工作。包括安装授权文件和创建一个实例并运行它。
首先,我们安装授权文件。
不安装授权文件的话,则是评估版,九十天后就要过期。授权文件,在安装包已经有了。我们用下面的命令安装它:
[root@Enterprise pe]# /opt/IBM/db2/V8.2/adm/db2licm -a /tmp/pe/db2/lic
ense/db2pe.lic
如果没有db2pe.lic 文件。可以去网上找找。
我们需要创建一个用户。
[root@Enterprise pe]# useradd db2inst1
[root@Enterprise pe]# passwd db2inst1
开始创建实例:
[root@Enterprise pe]# /opt/IBM/db2/V8.2/instance/db2icrt -u db2inst1 db2inst1
这样,我们就创建了一个实例db2ins1。
[root@Enterprise pe]# su db2inst1
[db2inst1@Enterprisepe]$ db2start
07/26/2007 16:45:10 0 0 SQL1063N DB2START processing was successful.
SQL1063N DB2START processing was successful.
这样我们数据库就启动成功。
[db2inst1@Enterprise pe]$ db2
(c) Copyright IBM Corporation 1993,2002
Command Line Processor for DB2 SDK 8.2.0
You can issue database manager commands and SQL statements from the command
prompt. For example:
db2 => connect to sample
db2 => bind sample.bnd
For general help, type: ?.
For command help, type: ? command, where command can be
the first few keywords of a database manager command. For example:
? CATALOG DATABASE for help on the CATALOG DATABASE command
? CATALOG for help on all of the CATALOG commands.
To exit db2 interactive mode, type QUIT at the command prompt. Outside
interactive mode, all commands must be prefixed with 'db2'.
To list the current command option settings, type LIST COMMAND OPTIONS.
For more detailed help, refer to the Online Reference Manual.
db2 =>
创建一个最简单的数据库
db2 => create database testdb
DB20000I The CREATE DATABASE command completed successfully.
查看本instance下有哪些database
db2 => list database directory
System Database Directory
Number of entries in the directory = 2
Database 1 entry:
Database alias = TESTDB
Database name = TESTDB
Local database directory = /home/db2inst1
Database release level = a.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
Database 2 entry:
Database alias = OMNIDB
Database name = OMNIDB
Local database directory = /home/db2inst1/data
Database release level = a.00
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
源文档 <http://crrok2002.blogbus.com/logs/7556671.html>
2. DB2的一些常用命令
一、基础篇
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 <表名> --查看表结构
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的字段进行值转换
例如: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的形式,如果不是需要进行转换,否则函数不能使用
例如:日期是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
原文地址 :http://www.blogjava.net/wuxj888java/archive/2008/01/16/175595.html
3. 安装文件时的产品简称的说明:
client | DB2 Client |
consv | DB2 Connect™ Enterprise Edition |
doce | DB2 Information Center |
ese | DB2 Enterprise Server Edition |
exp | DB2 Express Edition |
gse | DB2 Spatial Extender Comment = |
lsdc | WebSphere® Federation Server Non-Relational Wrappers |
nlpack | DB2 National Language Package |
pe | DB2 Personal Edition |
pe_connect | DB2 Connect Personal Edition |
qp | DB2 Query Patroller |
rcon | WebSphere Federation Server Relational Wrappers |
rtcl | DB2 Runtime Client |
wse | DB2 Workgroup Server Edition |
universal | Universal fix pack image that allows to you install the fix pack for multiple products simultaneously. |
源文档 <http://www-01.ibm.com/support/docview.wss?uid=swg21253570>