目录

简介

Oracle和DB2数据库对象的异同

把数据从Oracle移植到DB2的Java应用程序的详细分析

简介

在实际的软件项目的开发过程中,特别是在企业的应用系统集成(EAI)项目中广大开发人员经常遇到不同关系型数据库之间的数据移植问题。笔者根据自己在工作中的不同数据库数据移植的经验经过通用化的java应用程序,演示把数据从Oracle数据库移植到DB2数据库中。当然也可以应用其它支持JDBC和ODBC的关系型数据库。

Oracle和DB2数据库对象的异同

首先让大家快速的熟悉一下Oracle和DB2各个方面的异同

上面的表格比较全面的对比了二者之间的区别,相信大家应该对Oracle 和DB2在整体上有了比较全面的理解,好,下面就让大家对二个数据库的数据类型作详细的比较。

DB2和Oracle的数据类型比较

下面我通过一些具体的实例来详细演示DB2和Oracle的区别:

一、日期

Oracle的日期用法:

Create table date_demo
( day_demo varchar2(25),
actualDate DATE,
detaildate DATE
);插入数据
insert into date_demo values ('new years',to_date('01-02-2002','DD-MON-YYYY'),
to_date('01-02-2002','DD-MON-YYYY'),

...;查看结果:

select * from date_demo;
day_demo actualDate detaildate
------------- -------------- ------------
new years 01-02-2002 01-02-2002
new years 01-02-2002 01-02-2002

......;DB2的日期用法:

Create table date_demo
( day_demo char(25),
actualDate DATE,
detaildate DATE
);插入数据
insert into date_demo values ('new years',('01-02-2002'),
to_date('01-02-2002'),

......;查看结果:

select * from date_demo;
day_demo actualDate detaildate
--------------- -------------- ------------
new years 01-02-2002 01-02-2002
new years 01-02-2002 01-02-2002

......;二、序列

Oracle的序列用法:

create table dep (deptno smallint not null,
deptname varchar2(36) not null,
mgrno char(6),
admrdept smallint not null,
location char(30));
create sequence dept_seq start with 200 increment by 1;

接着

insert into dept values
(dept_seq.nextval,'sales','smith',55,'downtown'),
(dept_seq.nextval,'marketing','wong',12,'midtown'),
(dept_seq.nextval,'accounting','fisher',300,'uptown');
select * from dept;

DB2的序列用法:

首先建表:

create table dept
(deptno smallint not null generated always as identity(start with 200,
increment by 1),
deptname varchar(36) not null,
location char(30));

接下来插入数据

insert into dept values
(default,'sales','smith,50,'downtown'),
(default,'marketing','wong',23,'midtown'),
(default,'accounting','fisher'200,'uptown');

查询结果:

select * from dept;

结果和Oracle 的一样。

三、截断大的数据表的内容

Oracle在截断大的数据表提供了一个函数truncate,通过语句truncate table tab_name来清空表中的内容并重组表的空间,truncate 是DDL语句不能回滚。

DB2不支持truncate语句,但提供另外二种方法来解决上面的问题。

(1) 在建表时加选项not logged initially ,当清空表时通过alter table [name] activate not logged initially with empty table

(2)首先在操作系统上建一个空文件empty.del,接着通过import命令import from empty.del of del replace into [table_name]来清空表中的数据并重组表空间。

以上是我在使用Oracle 和DB2数据库的过程中总结的不通点,更多的不通可以查阅相关的手册。

通过上面的介绍相信大家对Oracle和DB2的区别会有一定程度的了解,下一篇将在这一篇的基础上介绍如何把Oracle中的数据移植到DB2数据库中来。

附DB2的存储过程

-- This is a CLP script that creates an SQL procedure.
-- To create the SQL procedure using this script, perform the following steps:
-- 1. connect to the database
-- 2. issue the command "db2 -td@ -vf "
-- where represents the name of this script
--
-- To call this SQL procedure from the command line, perform the following steps:
-- 1. connect to the database
-- 2. issue the following command:
-- db2 "CALL median_result_set (20000)"
--
-- The sample "rsultset.c" demonstrates how to call this SQL procedure using
-- a CLI client application.
CREATE PROCEDURE median_result_set
-- Declare medianSalary as INOUT so it can be used in DECLARE CURSOR
(INOUT medianSalary DOUBLE)
RESULT SETS 2
LANGUAGE SQL
BEGIN
DECLARE v_numRecords INT DEFAULT 1;
DECLARE v_counter INT DEFAULT 0;
DECLARE c1 CURSOR FOR
SELECT CAST(salary AS DOUBLE) FROM staff
ORDER BY salary;
-- use WITH RETURN in DECLARE CURSOR to return a result set
DECLARE c2 CURSOR WITH RETURN FOR
SELECT name, job, CAST(salary AS DOUBLE)
FROM staff
WHERE salary > medianSalary
ORDER BY salary;
-- you can return as many result sets as you like, just
-- ensure that the exact number is declared in the RESULT SETS
-- clause of the CREATE PROCEDURE statement
-- use WITH RETURN in DECLARE CURSOR to return another result set
DECLARE c3 CURSOR WITH RETURN FOR
SELECT name, job, CAST(salary AS DOUBLE)
FROM staff
WHERE salary < medianSalary
ORDER BY SALARY DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET medianSalary = 6666;
-- initialize OUT parameter
SET medianSalary = 0;
SELECT COUNT(*) INTO v_numRecords FROM STAFF;
OPEN c1;
WHILE v_counter < (v_numRecords / 2 + 1) DO
FETCH c1 INTO medianSalary;
SET v_counter = v_counter + 1;
END WHILE;
CLOSE c1;
-- return 1st result set, do not CLOSE cursor
OPEN c2;
-- return 2nd result set, do not CLOSE cursor
OPEN c3;
END @

把数据从Oracle移植到DB2的Java应用程序的详细分析