一、首先從網(wǎng)絡(luò)上找到一些資料如下:
1. 自動(dòng)增長(zhǎng)的數(shù)據(jù)類型處理
MYSQL有自動(dòng)增長(zhǎng)的數(shù)據(jù)類型,插入記錄時(shí)不用操作此字段,會(huì)自動(dòng)獲得數(shù)據(jù)值。Oracle沒有自動(dòng)增長(zhǎng)的數(shù)據(jù)類型,需要建立一個(gè)自動(dòng)增長(zhǎng)的序列號(hào),插入記錄時(shí)要把序列號(hào)的下一個(gè)值賦于此字段。
CREATE SEQUENCE 序列號(hào)的名稱 (最好是表名+序列號(hào)標(biāo)記) INCREMENT BY 1 START WITH 1 MAXVALUE 99999 CYCLE NOCACHE;
-
INSERT 語句插入這個(gè)字段值為: 序列號(hào)的名稱.NEXTVAL
2. 單引號(hào)的處理
MYSQL里可以用雙引號(hào)包起字符串,Oracle里只可以用單引號(hào)包起字符串。在插入和修改字符串前必須做單引號(hào)的替換:把所有出現(xiàn)的一個(gè)單引號(hào)替換成兩個(gè)單引號(hào)。當(dāng)然你如果使用 Convert Mysql to Oracle 工具就不用考慮這個(gè)問題
3.長(zhǎng)字符串的處理
在Oracle中,INSERT和UPDATE時(shí)最大可操作的字符串長(zhǎng)度小于等于4000個(gè)單字節(jié), 如果要插入更長(zhǎng)的字符串, 請(qǐng)考慮字段用CLOB類型,方法借用ORACLE里自帶的DBMS_LOB程序包。插入修改記錄前一定要做進(jìn)行非空和長(zhǎng)度判斷,不能為空的字段值和超出長(zhǎng)度字段值都應(yīng)該提出警告,返回上次操作。
4. 翻頁的SQL語句的處理
MYSQL處理翻頁的SQL語句比較簡(jiǎn)單,用LIMIT 開始位置, 記錄個(gè)數(shù)。Oracle處理翻頁的SQL語句就比較繁瑣了。每個(gè)結(jié)果集只有一個(gè)ROWNUM字段標(biāo)明它的位置, 并且只能用ROWNUM<100, 不能用ROWNUM>80。
以下是經(jīng)過分析后較好的兩種Oracle翻頁SQL語句( ID是唯一關(guān)鍵字的字段名 ):
語句一:SELECT ID, [FIELD_NAME,...] FROM TABLE_NAME WHERE ID IN ( SELECT ID FROM (SELECT ROWNUM AS NUMROW, ID FROM TABLE_NAME WHERE 條件1 ORDER BY 條件2) WHERE NUMROW > 80 AND NUMROW < 100 ) ORDER BY 條件3;
語句二:SELECT * FROM (( SELECT ROWNUM AS NUMROW, c.* from (select [FIELD_NAME,...] FROM TABLE_NAME WHERE 條件1 ORDER BY 條件2) c) WHERE NUMROW > 80 AND NUMROW < 100 ) ORDER BY 條件3;
5. 日期字段的處理
MYSQL日期字段分DATE和TIME兩種,Oracle日期字段只有DATE,包含年月日時(shí)分秒信息,用當(dāng)前數(shù)據(jù)庫的系統(tǒng)時(shí)間為SYSDATE, 精確到秒。
日期字段的數(shù)學(xué)運(yùn)算公式有很大的不同。MYSQL找到離當(dāng)前時(shí)間7天用 DATE_FIELD_NAME > SUBDATE(NOW(),INTERVAL 7 DAY)Oracle找到離當(dāng)前時(shí)間7天用 DATE_FIELD_NAME >SYSDATE - 7;
6. 字符串的模糊比較
MYSQL里用 字段名 like '%字符串%',Oracle里也可以用 字段名 like '%字符串%' 但這種方法不能使用索引, 速度不快,用字符串比較函數(shù) instr(字段名,'字符串')>0 會(huì)得到更精確的查找結(jié)果。
7. 空字符的處理
MYSQL的非空字段也有空的內(nèi)容,Oracle里定義了非空字段就不容許有空的內(nèi)容。按MYSQL的NOT NULL來定義ORACLE表結(jié)構(gòu), 導(dǎo)數(shù)據(jù)的時(shí)候會(huì)產(chǎn)生錯(cuò)誤。因此導(dǎo)數(shù)據(jù)時(shí)要對(duì)空字符進(jìn)行判斷,如果為NULL或空字符,需要把它改成一個(gè)空格的字符串。
以上內(nèi)容我作為參考。
二.工具的使用
網(wǎng)上好多朋友介紹使用Convert Mysql to Oracle這個(gè)工具,當(dāng)然能用工具解決的問題我們就用工具,關(guān)鍵是看工具能不能解決問題。通過工具會(huì)出現(xiàn)好多問題,最終還是要自己寫程式解決。后來發(fā)現(xiàn)工具導(dǎo)數(shù)據(jù)還是可以的,數(shù)據(jù)表的創(chuàng)建和修改只有自己寫程式解決了。但是導(dǎo)數(shù)據(jù)也有問題,如下:
導(dǎo)入數(shù)據(jù)遇到的問題
1、text到blob的時(shí)候,這個(gè)是影響很大的,不是我們希望看到的,就不要做多說明。
2、在Mysql中如果是Varchar或char中字符大小為2,意味著它可以輸入“12、中國、1中”等2個(gè)長(zhǎng)度的數(shù)據(jù),而在Oracle中是針對(duì)字節(jié)的,它只允許輸入英文字符2個(gè)或一個(gè)中文漢字,所以這變?cè)趯?dǎo)數(shù)據(jù)的時(shí)候要注意欄位的大小。
3、導(dǎo)入的過程中字符集必須要設(shè)置正確,否則會(huì)出現(xiàn)亂碼的數(shù)據(jù)。
4、index是不可以導(dǎo)進(jìn)來的,要注意table是否有Index;是否允許NULL值也要注意。
5、Mysql中id自動(dòng)增長(zhǎng)的table要做處理,在Oracle中設(shè)置相關(guān)的sequence和trigger。
6、comment在Oracle中是關(guān)鍵字,不能當(dāng)做列來處理。
7、當(dāng)數(shù)據(jù)量大的時(shí)候做特別處理。
三.自己寫程式解決問題
//獲得所有table的名字
SELECT
`TABLES`.`TABLE_SCHEMA`, `TABLES`.`TABLE_NAME`
FROM
`information_schema`.`TABLES`
WHERE
`TABLES`.`TABLE_TYPE` = 'base table'
and `TABLES`.`TABLE_SCHEMA` ='netoffice';
//獲得某table所有列的信息
SELECT * FROM
`information_schema`.`COLUMNS`
where `TABLE_SCHEMA`='netoffice'
and `TABLE_NAME`='drmcertification' order by `ORDINAL_POSITION`;
//java程式:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
public class TestMysql {
public static Connection conn;
public static Statement statement;
public Hashtable<String, Vector<Vector<String>>> hashtable = new Hashtable<String, Vector<Vector<String>>>();
public static final String filepathCreateTable = "D://CreateTable.txt";
public static final String filepathCreateSequence = "D://CreateSequence.txt";
public static final String filepathCreateTrigger = "D://CreateTrigger.txt";
public static final String filepathCreatePrimarykey = "D://CreatePrimarykey.txt";
public static final String filepathCreateIndex = "D://CreateIndex.txt";
//只要修改主機(jī)名,數(shù)據(jù)庫名字和user、password
public static final String DBdriver = "com.mysql.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://主機(jī)地址:3306/數(shù)據(jù)庫名字?user=roots&password=1234";
public static final String DBSCHEMA = "數(shù)據(jù)庫名字"; //
public static void main(String[] args) {
new TestMysql();
}
public TestMysql() {
//刪除文件
deleteFile();
if (!connectionMethod()) {
System.out.println("鏈接錯(cuò)誤");
return;
}
Vector<String> table = queryAllTable(DBSCHEMA);
if (table.size() == 0) {
System.out.println("沒有找到表");
return;
}
for (int i = 0; i < table.size(); i++) { //得到數(shù)據(jù)
hashtable.put(table.get(i), handle_table(table.get(i)));
}
// hashtable.put(table.get(0).toString(),handle_table(table.get(0)));
System.out.println("操作正在進(jìn)行中,請(qǐng)耐心等待......");
generatorString(hashtable); //產(chǎn)生字符串
close();//關(guān)閉連接
System.out.println("finish");
}
public void generatorString(Hashtable hashtable) {
Iterator iter = hashtable.keySet().iterator();
while (iter.hasNext()) {
String tablescript = ""; // 創(chuàng)表語句
String tablesequence = ""; // 建立sequence
String tabletrigger = ""; // 建立trigger
String tableprimarykey = "";// 建立主鍵
String tableindex = "";// 建立索引
String primarkeyColumn = "";
String indexColumn = "";
int primarykey = 0;
int index = 0;
String tablename = (String) iter.next();
Vector valall = (Vector) hashtable.get(tablename);
tablescript = "create table " + tablename + "(";
for (int i = 0; i < valall.size(); i++) {
Vector<String> val = (Vector) valall.get(i);
String column_name = val.get(0).toString();// 列名
String is_nullable = val.get(1).toString();// 是否為空,如果不允許NO,允許為YES
String data_type = val.get(2).toString();// int,varchar,text,timestamp,date
String character_maximun_length = val.get(3).toString();// 長(zhǎng)度大小
String column_key = val.get(4).toString();// 是否主鍵 是的話為PRI
// MUL(index)
// 有兩個(gè)PRI說明是複合index
String extra = val.get(5).toString(); // 是否自動(dòng)增長(zhǎng)列 是的話
// auto_increment
String column_default = val.get(6).toString();// 是否有默認(rèn)值
if (data_type.equals("varchar") || data_type.equals("char")) { // 驗(yàn)證是否有中文字符
if (judge_china(tablename, column_name)) {
character_maximun_length = Integer
.parseInt(character_maximun_length)
* 3 + "";
}
}
tablescript = tablescript + column_name + " ";
if (data_type.equals("int")) {
tablescript = tablescript + "NUMBER" + " ";
} else if (data_type.equals("mediumint")) {
tablescript = tablescript + "NUMBER" + " ";
} else if (data_type.equals("char")) {
tablescript = tablescript + "varchar2("
+ character_maximun_length + ")" + " ";
} else if (data_type.equals("varchar")) {
tablescript = tablescript + "varchar2("
+ character_maximun_length + ")" + " ";
} else if (data_type.equals("text")) {
tablescript = tablescript + "varchar2(4000) ";
} else if (data_type.equals("timestamp")) {
tablescript = tablescript + "date" + " ";
} else if (data_type.equals("date")) {
tablescript = tablescript + "date" + " ";
} else if (data_type.equals("float")) {
tablescript = tablescript + "NUMBER" + " ";
} else if (data_type.equals("longtext")) {
tablescript = tablescript + "varchar2(4000) ";
} else if (data_type.equals("smallint")) {
tablescript = tablescript + "NUMBER" + " ";
} else if (data_type.equals("double")) {
tablescript = tablescript + "NUMBER" + " ";
} else if (data_type.equals("datetime")) {
tablescript = tablescript + "date" + " ";
}
if (column_default.length() > 0) { // 是否有默認(rèn)值
if (column_default.equals("CURRENT_TIMESTAMP")) {
tablescript = tablescript + "default sysdate" + " ";
} else {
tablescript = tablescript + "default " + column_default
+ " ";
}
}
if (is_nullable.equals("NO")) { // 是否為空值
tablescript = tablescript + "not null,";
} else {
tablescript = tablescript + ",";
}
if (extra.equals("auto_increment")) { // 是否自動(dòng)增長(zhǎng)列
int maxid = get_maxId(tablename, column_name);
tablesequence = "create sequence sq_" + tablename + " "
+ "minvalue " + maxid + " "
+ "maxvalue 9999999999999999 " + "increment by 1 "
+ "start with " + maxid + " " + "cache 20;";
tabletrigger = "EXECUTE IMMEDIATE 'create trigger tr_"
+ tablename + " " + "before " + "insert on "
+ tablename + " for each row " + "begin "
+ "select sq_" + tablename + ".nextval into:new."
+ column_name + " from dual; " + "end;';";
}
if (column_key.length() > 0) {
if (column_key.equals("PRI")) {
primarykey++;
primarkeyColumn = primarkeyColumn + column_name + ",";
} else if (column_key.equals("MUL")) {
index++;
indexColumn = indexColumn + column_name + ",";
}
}
}
if (primarykey == 1) {
primarkeyColumn = primarkeyColumn.substring(0, primarkeyColumn
.length() - 1);
String key = "pr_" + tablename + "_" + primarkeyColumn;
if (key.length() > 30) {
key = "pr_" + primarkeyColumn;
}
tableprimarykey = "alter table " + tablename
+ " add constraint " + key + " primary key ("
+ primarkeyColumn + ");";
} else {
primarkeyColumn = primarkeyColumn.substring(0, primarkeyColumn
.length() - 1);
String indextemp = tablename + "_index";
if (indextemp.length() > 30)
indextemp = primarkeyColumn.replace(',', '_') + "_index";
tableindex = "create index " + indextemp + " on " + tablename
+ " (" + primarkeyColumn + ");";
}
if (index > 0) {
indexColumn = indexColumn
.substring(0, indexColumn.length() - 1);
String indextemp = tablename + "_index";
if (indextemp.length() > 30)
indextemp = indexColumn.replace(',', '_') + "_index";
tableindex = "create index " + indextemp + " on " + tablename
+ " (" + indexColumn + ");";
}
tablescript = tablescript.substring(0, tablescript.length() - 1);
tablescript = tablescript + ");";
if (tablescript.length() > 0)
write(filepathCreateTable, tablescript);
if (tablesequence.length() > 0)
write(filepathCreateSequence, tablesequence);
if (tabletrigger.length() > 0)
write(filepathCreateTrigger, tabletrigger);
if (tableprimarykey.length() > 0)
write(filepathCreatePrimarykey, tableprimarykey);
if (tableindex.length() > 0)
write(filepathCreateIndex, tableindex);
}
}
public void close() {
try {
statement.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Vector<Vector<String>> handle_table(String tablename) {
Vector<Vector<String>> arg = new Vector<Vector<String>>();
try {
String queryDetail = "SELECT * "
+ "FROM `information_schema`.`COLUMNS` "
+ "where `TABLE_SCHEMA`='" + DBSCHEMA + "' "
+ "and `TABLE_NAME`='" + tablename + "' "
+ "order by `ORDINAL_POSITION`";
// System.out.println("sql= "+queryDetail);
ResultSet rst = statement.executeQuery(queryDetail);
while (rst.next()) {
Vector<String> vec = new Vector<String>();
String column_name = NulltoSpace(rst.getString("COLUMN_NAME"));// 列名
String is_nullable = NulltoSpace(rst.getString("IS_NULLABLE"));// 是否為空,如果不允許NO,允許為YES
String data_type = NulltoSpace(rst.getString("DATA_TYPE"));// int,varchar,text,timestamp,date
String character_maximun_length = NulltoSpace(rst
.getString("CHARACTER_MAXIMUM_LENGTH"));// 長(zhǎng)度大小
String column_key = NulltoSpace(rst.getString("COLUMN_KEY"));// 是否主鍵
// 是的話為PRI
// MUL(index)
// 有兩個(gè)PRI說明是複合index
String extra = NulltoSpace(rst.getString("EXTRA")); // 是否自動(dòng)增長(zhǎng)列
// 是的話
// auto_increment
String column_default = NulltoSpace(rst
.getString("COLUMN_DEFAULT"));// 是否有默認(rèn)值
vec.add(column_name);
vec.add(is_nullable);
vec.add(data_type);
vec.add(character_maximun_length);
vec.add(column_key);
vec.add(extra);
vec.add(column_default);
arg.add(vec);
}
rst.close();
} catch (SQLException e) {
e.printStackTrace();
}
return arg;
}
public boolean judge_china(String tablename, String columnname) {
try {
String querysql = "select count(1) row from " + tablename
+ " where length(" + columnname + ")!=char_length("
+ columnname + ")";
// System.out.println("sql= "+querysql);
ResultSet rst = statement.executeQuery(querysql);
if (rst.next()) {
if (NulltoSpace(rst.getString("row")).equals("0")) {
return false;
} else {
return true;
}
}
rst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
}
return true;
}
public int get_maxId(String tablename, String columnname) {
String maxValue = "0";
try {
String querysql = "select max(" + columnname + ") maxid from "
+ tablename;
// System.out.println("sql= "+querysql);
ResultSet rst = statement.executeQuery(querysql);
if (rst.next()) {
maxValue = NulltoSpace(rst.getString("maxid"));
}
rst.close();
} catch (SQLException e) {
}
return Integer.parseInt(maxValue + 1);
}
public Vector<String> queryAllTable(String table_schema) {
Vector<String> tableName = new Vector<String>();
try {
String queryTable = "SELECT `TABLES`.`TABLE_NAME` "
+ "FROM `information_schema`.`TABLES` "
+ "WHERE `TABLES`.`TABLE_TYPE` = 'base table' "
+ "and `TABLES`.`TABLE_SCHEMA` ='" + table_schema + "'";
// System.out.println("sql= "+queryTable);
ResultSet rst = statement.executeQuery(queryTable);
while (rst.next()) {
tableName.add(NulltoSpace(rst.getString("TABLE_NAME")));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
}
return tableName;
}
public boolean connectionMethod() {
try {
Class.forName(DBdriver).newInstance();
conn = DriverManager.getConnection(DBURL);
statement = conn.createStatement();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public static String NulltoSpace(Object o) {
if (o == null)
return "";
else if (o.equals("null")) {
return "";
} else {
return o.toString().trim();
}
}
public static void deleteFile(){
File f;
f= new File(filepathCreateTable);
if(f.exists()) f.delete();
f= new File(filepathCreatePrimarykey);
if(f.exists()) f.delete();
f= new File(filepathCreateSequence);
if(f.exists()) f.delete();
f= new File(filepathCreateTrigger);
if(f.exists()) f.delete();
f= new File(filepathCreateIndex);
if(f.exists()) f.delete();
}
public static void write(String path, String content) {
String s = new String();
String s1 = new String();
try {
File f = new File(path);
if (f.exists()) {
} else {
f.createNewFile();
}
BufferedReader input = new BufferedReader(new FileReader(f));
while ((s = input.readLine()) != null) {
s1 += s + "\r\n";
}
input.close();
s1 += content;
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} 本文出自:億恩科技【www.allwellnessguide.com】
服務(wù)器租用/服務(wù)器托管中國五強(qiáng)!虛擬主機(jī)域名注冊(cè)頂級(jí)提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]
|