1. 程式人生 > >Mysql切換Oracle資料庫

Mysql切換Oracle資料庫

一、Oracle建立資料庫:

     1.1建表空間操作:
     1.1.1建立表空間:create tablespace test_space datafile 'D://oracle/test.dbf' size 500M autoextends on next 50M maxsize 5000M;
     1.1.2刪除表空間:drop tablespace test_space including contents and datafiles;
     1.1.3查看錶空間:select * from dba_tablespaces,dba_data_files;
   1.2.操作使用者:
      1.2.1建立使用者:

create user test001 identified by '密碼' default tablespace test_space;
      1.2.2授權使用者:
        alter user test001 quota unlimited on test_space;授權使用者test001使用test_space表空間
        grant create session to test001;授予test001使用者建立session的許可權,即登入許可權
        grant unlimited tablespace to test001;授權test001使用表空間的許可權
      1.2.3檢視使用者許可權:
          select * from user_sys_privs;檢視當前使用者所有許可權

          select * from user_tab_privs;檢視所用使用者對錶的許可權

二、切換資料庫,資料的轉移:

2.1使用sqlDeveloper連線Oracle資料庫;
   2.2在\sqldeveloper\jlib目錄中新增mysql-connector-java-5.1.41-bin.jar包,重啟sqlDeveloper即可連線mysql資料庫了;
   2.3查詢到mysql中對應資料庫,找到需要轉移資料的表,右鍵即可出現複製到Oracle中,目標連線名稱為你所操作的oracle使用者所對應的資料庫名稱。
   2.4.檢視oracle中對應的表的欄位,資料等資訊。會出現有某些欄位型別不對應的情況。

三、資料庫欄位型別不對應:

3.1修改欄位型別不對應的列名(這一列後續會刪除,修改後的列名只要能識別即可),執行sql語句:     alter table test_table rename column id to id123;     3.2新增列,列名為欄位不對應的列名,欄位型別為你所需要的型別,執行sql語句:     alter table test_table add id varchar2(150);     3.3將修改列名後的列資料複製到對應的新增列中,注意複製時資料型別的轉換,執行sql語句:    update test_table set id=cast(id123 as carchar2(150));     3.4資料複製完成後,將之前更改了列名的列刪除即可,執行SQL語句:     alter table test_table drop column id123;     3.5執行任意sql,檢查是否更新正確。

四、 SQL語句的修改:

4.1自增主鍵oracle無法自動完成,需要利用序列或者觸發器實現主鍵自增(本人利用sequence序列):
   4.1.1檢視sequence序列:select  * from dba_sequences where sequence_owner=使用者名稱;
        說明:where為查詢對應的使用者下的所有sequence,不新增where新增查詢到的是所有的sequence

                 4.1.2建立sequence序列

   create sequence test_id_seq  increment by 1 start with 100 maxvalue 10000 cycle;

        說明: incremnet by 1 ->主鍵每次增加1;start with 100->主鍵從100開始計數;maxvalue 10000->主鍵最大值為10000(如果不需要可設定為nomaxvalue);cycle->累加迴圈,主鍵值到maxvalue後迴歸到start with設定的值進行迴圈(可設定為nocycle不迴圈);
4.1.3修改sequence序列:alter sequence test_id_seq inrement by 3 nomaxvalue nocycle;如果需要修改起始值(start with的值),需要刪除掉sequence序列重新建立;
4.1.4 刪除sequence序列:drop sequence test_id_seq;
   4.2獲取自增主鍵的值:
      4.2.1在mysql中使用:
          select @@Identity可以直接獲取到剛剛插入的主鍵id,在oracle中無法識別這條sql語句;
      4.2.2在oracle中執行:
          select test_id_seq.CURRVAL from dual;
          select test_id_seq.NEXTVAL from dual;
          來獲取主鍵id;
        注意:必須執行一次獲取.CURRVAL後才可執行.NEXTVAL,否則報錯。