mysql有自动增长的数据类型,插入记录时不用操作此字段,会自动获得数据值。oracle没有自动增长的数据类型,需要建立一个自动增长
一、首先從網絡上找到一些資料如下:
1. 自动增长的数据类型处理
mysql有自动增长的数据类型,插入记录时不用操作此字段,会自动获得数据值。oracle没有自动增长的数据类型,需要建立一个自动增长的序列号,插入记录时要把序列号的下一个值赋于此字段。
create sequence 序列号的名称 (最好是表名+序列号标记) increment by 1 start with 1 maxvalue 99999 cycle nocache;
insert 语句插入这个字段值为: 序列号的名称.nextval
2. 单引号的处理
mysql里可以用双引号包起字符串,oracle里只可以用单引号包起字符串。在插入和修改字符串前必须做单引号的替换:把所有出现的一个单引号替换成两个单引号。当然你如果使用 convert mysql to oracle 工具就不用考虑这个问题
3.长字符串的处理
在oracle中,insert和update时最大可操作的字符串长度小于等于4000个单字节, 如果要插入更长的字符串, 请考虑字段用clob类型,方法借用oracle里自带的dbms_lob程序包。插入修改记录前一定要做进行非空和长度判断,不能为空的字段值和超出长度字段值都应该提出警告,返回上次操作。
4. 翻页的sql语句的处理
mysql处理翻页的sql语句比较简单,用limit 开始位置, 记录个数。oracle处理翻页的sql语句就比较繁琐了。每个结果集只有一个rownum字段标明它的位置, 并且只能用rownum80。
以下是经过分析后较好的两种oracle翻页sql语句( id是唯一关键字的字段名 ):
语句一: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
语句二: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
5. 日期字段的处理
mysql日期字段分date和time两种,oracle日期字段只有date,包含年月日时分秒信息,用当前数据库的系统时间为sysdate, 精确到秒。
日期字段的数学运算公式有很大的不同。mysql找到离当前时间7天用 date_field_name > subdate(now(),interval 7 day)oracle找到离当前时间7天用 date_field_name >sysdate - 7;
6. 字符串的模糊比较
mysql里用 字段名 like '%字符串%',oracle里也可以用 字段名 like '%字符串%' 但这种方法不能使用索引, 速度不快,用字符串比较函数 instr(字段名,'字符串')>0 会得到更精确的查找结果。
7. 空字符的处理
mysql的非空字段也有空的内容,oracle里定义了非空字段就不容许有空的内容。按mysql的not null来定义oracle表结构, 导数据的时候会产生错误。因此导数据时要对空字符进行判断,,如果为null或空字符,需要把它改成一个空格的字符串。
以上內容我作為參考。
二.工具的使用
網上好多朋友介紹使用convert mysql to oracle這個工具,當然能用工具解決的問題我們就用工具,關鍵是看工具能不能解決問題。通过工具会出现好多问题,最终还是要自己写程式解决。后来发现工具导数据还是可以的,数据表的创建和修改只有自己写程式解决了。但是导数据也有问题,如下:
導入數據遇到的問題
1、text到blob的時候,這個是影響很大的,不是我們希望看到的,就不要做多說明。
2、在mysql中如果是varchar或char中字符大小為2,意味著它可以輸入“12、中國、1中”等2個長度的數據,而在oracle中是針對字節的,它只允許輸入英文字符2個或一個中文漢字,所以這變在導數據的時候要注意欄位的大小。
3、導入的過程中字符集必須要設置正確,否則會出現亂碼的數據。
4、index是不可以導進來的,要注意table是否有index;是否允許null值也要注意。
5、mysql中id自動增長的table要做處理,在oracle中設置相關的sequence和trigger。
6、comment在oracle中是關鍵字,不能當做列來處理。
7、當數據量大的時候做特別處理。
三.自己写程式解决问题
//获得所有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>> hashtable = new hashtable>>();
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;
//只要修改主機名,數據庫名字和user、password
public static final string dbdriver = com.mysql.jdbc.driver;
public static final string dburl = jdbc:mysql://主機地址:3306/數據庫名字?user=roots&password=1234;
public static final string dbschema = 數據庫名字; //
public static void main(string[] args) {
new testmysql();
}
public testmysql() {
//刪除文件
deletefile();
if (!connectionmethod()) {
system.out.println(鏈接錯誤);
return;
}
vector table = queryalltable(dbschema);
if (table.size() == 0) {
system.out.println(沒有找到表);
return;
}
for (int i = 0; i hashtable.put(table.get(i), handle_table(table.get(i)));
}
// hashtable.put(table.get(0).tostring(),handle_table(table.get(0)));
system.out.println(操作正在進行中,請耐心等待......);
generatorstring(hashtable); //產生字符串
close();//關閉連接
system.out.println(finish);
}
public void generatorstring(hashtable hashtable) {
iterator iter = hashtable.keyset().iterator();
while (iter.hasnext()) {
string tablescript = ; // 創表語句
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 vector 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();// 長度大小
string column_key = val.get(4).tostring();// 是否主鍵 是的話為pri
// mul(index)
// 有兩個pri說明是複合index
string extra = val.get(5).tostring(); // 是否自動增長列 是的話
// auto_increment
string column_default = val.get(6).tostring();// 是否有默認值
if (data_type.equals(varchar) || data_type.equals(char)) { // 驗證是否有中文字符
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) { // 是否有默認值
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)) { // 是否自動增長列
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> handle_table(string tablename) {
vector> arg = new vector>();
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 vec = new vector();
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));// 長度大小
string column_key = nulltospace(rst.getstring(column_key));// 是否主鍵
// 是的話為pri
// mul(index)
// 有兩個pri說明是複合index
string extra = nulltospace(rst.getstring(extra)); // 是否自動增長列
// 是的話
// auto_increment
string column_default = nulltospace(rst
.getstring(column_default));// 是否有默認值
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 queryalltable(string table_schema) {
vector tablename = new vector();
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();
}
}
}
