oracle移植序列,Oracle迁移之批量迁移INDEX、SEQUENCE、VIEW

最近碰到oracle数据库迁移,由于高级的迁移方法还不会,当前只会手动导入导出迁移。在迁移完所有数据表后,需要继续迁移索引、序列、视图、触发器、函数等。

本文主要说明批量迁移索引、序列、视图。触发器由于执行过程中异常,还没有找到方法自动处理;函数就只有几个手动处理掉了。

1. java程序

本次迁移面临问题:索引较多有68个,序列28个,视图8个。手动一个一个处理,看得眼睛都花了。就弄个java脚本处理。

不过各位碰到具体问题需要做微调。

package com.autonavi.service;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.sqlException;

import java.sql.sqlSyntaxErrorException;

import java.sql.Statement;

import com.autonavi.db.DataBaseConnection;

public class DealOracle {

private static DataBaseConnection cFactory = new DataBaseConnection();

static ResultSet rs = null;

static Connection connection1 = null;

static Connection connection2 = null;

static Statement statement = null;

public static void copyIndex(){

// 1.查询出建索引语句

String sql = "SELECT DBMS_MetaDATA.GET_DDL('INDEX',u.index_name) AS id FROM USER_INDEXES u";

try {

int count = 0;

PreparedStatement pState1 = null;

PreparedStatement pState2 = null;

pState1 = cFactory.createConnection4 ().prepareStatement(sql);

ResultSet rs = pState1.executeQuery();

while(rs.next()){

String str = rs.getString("ID");

String newstr = "";

if(str.contains("(DEGREE 0 INSTANCES 0)") == false){

count++;

String substr = str.substring(0,str.indexOf("TABLESPACE"));

newstr = substr+"TABLESPACE \"RUS\"";

//System.out.println("str:"+str);

System.out.println("newstr:"+newstr);

System.out.println("count:"+count);

try {

pState2 = cFactory.createConnection3().prepareStatement(newstr);

pState2.executeQuery(newstr);

pState2.close();

System.out.println("新建索引成功");

} catch (sqlSyntaxErrorException e) {

e.printStackTrace();

} catch (sqlException e) {

e.printStackTrace();

}

}

}

pState1.close();

} catch (sqlException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

cFactory.releaseConnection4();

cFactory.releaseConnection3();

}

}

public static void copySequence(){

// 1.查询出建序列语句

String sql = "SELECT DBMS_MetaDATA.GET_DDL('SEQUENCE',u.sequence_name) AS id FROM USER_SEQUENCES u";

try {

int count = 0;

PreparedStatement pState1 = null;

PreparedStatement pState2 = null;

pState1 = cFactory.createConnection4 ().prepareStatement(sql);

ResultSet rs = pState1.executeQuery();

while(rs.next()){

String str = rs.getString("ID");

count++;

System.out.println("sequence:"+str);

System.out.println("count:"+count);

try {

pState2 = cFactory.createConnection3().prepareStatement(str);

pState2.executeQuery(str);

pState2.close();

System.out.println("新建序列成功");

} catch (sqlSyntaxErrorException e) {

e.printStackTrace();

} catch (sqlException e) {

e.printStackTrace();

}

}

pState1.close();

} catch (sqlException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

cFactory.releaseConnection4();

cFactory.releaseConnection3();

}

}

public static void copyView(){

// 1.查询出建VIEW语句

String sql = "SELECT DBMS_MetaDATA.GET_DDL('VIEW',u.view_name) AS id FROM USER_VIEWS u";

try {

int count = 0;

PreparedStatement pState1 = null;

PreparedStatement pState2 = null;

pState1 = cFactory.createConnection4 ().prepareStatement(sql);

ResultSet rs = pState1.executeQuery();

while(rs.next()){

String str = rs.getString("ID");

count++;

System.out.println("view:"+str);

System.out.println("count:"+count);

try {

pState2 = cFactory.createConnection3().prepareStatement(str);

pState2.executeQuery(str);

pState2.close();

System.out.println("新建view成功");

} catch (sqlSyntaxErrorException e) {

e.printStackTrace();

} catch (sqlException e) {

e.printStackTrace();

}

}

pState1.close();

} catch (sqlException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

cFactory.releaseConnection4();

cFactory.releaseConnection3();

}

}

// 此方法有问题,不行。不过可以先通过下面的语句查询出触发器,手动创建。

public static void copyTriger(){

// 1.查询出建触发器语句

String sql = "SELECT DBMS_MetaDATA.GET_DDL('TRIGGER',u.trigger_name) AS id FROM USER_TRIGGERS u";

try {

int count = 0;

PreparedStatement pState1 = null;

PreparedStatement pState2 = null;

pState1 = cFactory.createConnection4 ().prepareStatement(sql);

ResultSet rs = pState1.executeQuery();

while(rs.next()){

String str = rs.getString("ID");

String newstr1 = "";

String newstr2 = "";

System.out.println("str:"+str);

newstr1 = str.substring(0,str.indexOf("ALTER"));

newstr2 = str.substring(str.indexOf("ALTER"));

count++;

System.out.println("trigger1:"+newstr1);

System.out.println("trigger2:"+newstr2);

System.out.println("count:"+count);

try {

pState2 = cFactory.createConnection3().prepareCall(newstr1);

pState2.executeUpdate();

//pState2 = cFactory.createConnection3().prepareStatement(newstr2);

//pState2.execute();

pState2.close();

System.out.println("新建触发器成功");

} catch (sqlSyntaxErrorException e) {

e.printStackTrace();

} catch (sqlException e) {

e.printStackTrace();

}

}

pState1.close();

} catch (sqlException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

cFactory.releaseConnection4();

cFactory.releaseConnection3();

}

}

public static void copyFunction(){

}

public static void main(String[] args){

copyView();

}

}

2. 处理结果

2.1. 创建索引

索引创建成功

如果之前已经有这个索引,提示重名。

2.2. 创建序列

序列创建成功

2.3. 创建视图

视图创建成功

3. 待解问题

java 处理触发器这块还没有弄清楚,望后续有时间了解下这方面的方法。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 Oracle 序列迁移到 MySQL,需要考虑到两个数据库之间的差异。在 Oracle 中,序列是自动递增的整数值生成器,而 MySQL 中没有直接的序列类型。以下是一个迁移序列的一般步骤: 1. 创建一个 MySQL 表来模拟序列:在 MySQL 中,您可以创建一个包含一个自增列的表来模拟序列。例如,创建一个名为 `sequence_table` 的表,其中包含一个名为 `sequence_value` 的自增列。 2. 导出 Oracle 序列的当前值:使用 Oracle SQL 查询来获取序列的当前值。例如,对于名为 `my_sequence` 的序列,您可以运行如下查询:`SELECT my_sequence.CURRVAL FROM dual;`。 3. 将导出的当前值插入到模拟序列表中:将步骤 2 中导出的序列当前值插入到 MySQL 的模拟序列表中。 例如,使用以下查询将序列当前值插入到 `sequence_table` 表中: ```sql INSERT INTO sequence_table (sequence_value) VALUES (<当前值>); ``` 4. 创建一个在 MySQL 中自增的列:在需要使用序列的 MySQL 表中,创建一个自增列,以模拟 Oracle 序列的行为。例如,创建一个名为 `id` 的自增列。 5. 更新表的自增列的起始值:使用 `ALTER TABLE` 语句来更新自增列的起始值,以与步骤 2 中导出的当前值匹配。 例如,使用以下查询将 `id` 列的起始值更新为模拟序列表中的当前值: ```sql ALTER TABLE your_table AUTO_INCREMENT = <当前值>; ``` 现在,您可以使用 MySQL 表中的自增列来模拟 Oracle 序列的行为。请注意,这只是一种模拟方法,可能无法完全复制 Oracle 序列的所有行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值