1. <var id="fe6gj"></var>

    <rp id="fe6gj"><nav id="fe6gj"></nav></rp>

    <noframes id="fe6gj"><cite id="fe6gj"></cite>

    <ins id="fe6gj"><button id="fe6gj"><p id="fe6gj"></p></button></ins>
    1. <tt id="fe6gj"><i id="fe6gj"><sub id="fe6gj"></sub></i></tt>
        始創于2000年 股票代碼:831685
        咨詢熱線:0371-60135900 注冊有禮 登錄
        • 掛牌上市企業
        • 60秒人工響應
        • 99.99%連通率
        • 7*24h人工
        • 故障100倍補償
        您的位置: 網站首頁 > 幫助中心>文章內容

        MySQL轉數據到Oracle

        發布時間:  2012/8/23 16:43:24

        一、首先從網絡上找到一些資料如下:
        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字段標明它的位置, 并且只能用ROWNUM<100, 不能用ROWNUM>80。

        以下是經過分析后較好的兩種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 < 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,包含年月日時分秒信息,用當前數據庫的系統時間為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<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";
         
         //只要修改主機名,數據庫名字和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<String> table = queryAllTable(DBSCHEMA);
          if (table.size() == 0) {
           System.out.println("沒有找到表");
           return;
          }

          for (int i = 0; i < table.size(); 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 < 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();// 長度大小
            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<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"));// 長度大小
            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<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.endtimedelusion.com】

        服務器租用/服務器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質保障!--億恩科技[ENKJ.COM]

      1. 您可能在找
      2. 億恩北京公司:
      3. 經營性ICP/ISP證:京B2-20150015
      4. 億恩鄭州公司:
      5. 經營性ICP/ISP/IDC證:豫B1.B2-20060070
      6. 億恩南昌公司:
      7. 經營性ICP/ISP證:贛B2-20080012
      8. 服務器/云主機 24小時售后服務電話:0371-60135900
      9. 虛擬主機/智能建站 24小時售后服務電話:0371-60135900
      10. 專注服務器托管17年
        掃掃關注-微信公眾號
        0371-60135900
        Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權所有  地址:鄭州市高新區翠竹街1號總部企業基地億恩大廈  法律顧問:河南亞太人律師事務所郝建鋒、杜慧月律師   京公網安備41019702002023號
          0
         
         
         
         

        0371-60135900
        7*24小時客服服務熱線

         
         
        av不卡不卡在线观看_最近2018年中文字幕_亚洲欧美一区二区三区_一级A爱做片免费观看国产_日韩在线中文天天更新_伊人中文无码在线