Mysql遷移到Oracle踩坑

公司要求兼容Oracle,將踩過的坑共享出來供大家參考。

1. limit轉換

mysql:
selelct a,b,c from tableA where a=1 order by b limit 1,10;
oracle:
select * from (
select A.*, rowum r from (
	select a,b,c from tableA where a=1 order by b
) A
where rownum <![CDATA[ <= ]]> 10
) B 
where r > 1;

2. insert坑

mysql:
insert into tableA (id,name,type) value (1,2,3);
insert into tableA (id,name,type) values(1,2,3);
oracle:
insert into tableA (id,name,type) values (1,2,3);

3. group by

mysql:
select a,b,c from tableA where a = 1 group by b;
select count(1),b from tableA group by a;
oracle:

有group by時,select子句中要麼只有聚合函數或爲 “*”,要麼必須包含group的子句(且要比其他子句出現的早);

select b,a,c from tableA where a =1 group by b;
select count(1),a,b from tableA group by a;

4. 日期

mysql:
select * from tableA where a > "2020-01-01 13:10:00";
select * from tableA where b = "2020-01-01 13:10:00";
oracle:
-- 12小時制的pattern寫作'yyyy-MM-dd hh12:mi:ss',24小時制寫作'yyyy-MM-dd hh24:mi:ss'
select * from tableA where a > to_date('2020-01-01 13:10:00','yyyy-MM-dd hh24:mi:ss');
-- 當b是varchar2類型時:
select * from tableA where b = '2020-01-01 13:10:00';
-- 當傳來的參數是date類型,表中數據爲varchar2類型時
select * from tableA where b = to_char('2020-01-01 13:10:00','yyyy-MM-dd hh24:mi:ss');

5. concat()函數

mysql:
select * from tableA where a like concat('%',#{a},'%');
oracle:
-- 注意:Oracle是單引號,Oracle的單雙引號意義不同
select * from tableA where a like concat(concat('%',#{a}),'%');

6. 單引號雙引號

mysql:
select * from tableA where a like concat("%",#{a},"%");
select * from tableA where a like concat('%',#{a},'%');
oracle:
-- 注意這裏是單引號
select * from tableA where a like concat(concat('%',#{a}),'%');

7. 字段名爲date

mysql:
select * from tableA where date = #{date};
oracle:
-- 這裏是雙引號
select * from tableA where "date" = #{date};

8. left()函數

mysql:
select left(a,4) from tableA;
oracle:
select substr(a,0,4) from tableA;

9. 解決id自增問題

-- 在表中創建sequence和對應的觸發器,在向指定表插入數據時會自動填入id;

-- 避免重複,若沒有創建過,可以不添加這句話
drop sequence tableA_id;

-- 創建sequence
create sequence tableA_id
        -- id的最小值
        minvalue 1  
        -- id的最大值,若沒有或者不確定可以用 nomaxvalue
        maxvalue 9999999
        -- 遞增量
        increment by 1     
        -- id的起始值
        start with 1
        -- 是否循環
        nocycle
        -- 是否設置緩存
        nocache; 

-- 創建觸發器
create or replace trigger tableA_id_trigger
-- 將觸發器與tableA的insert事件綁定
before insert on tableA for each row
begin
-- tableA_id.nextval即爲自動生成的id
select tableA_id.nextval into:new.id from dual;
end;
在 mapper.xml 中有填寫id字段需求時也可以在有sequence的前提下寫作:
<!-- tableA_id是數據庫中已經創建的sequence -->
<insert id="insertTest" parameterType="java.util.Map" keyProperty="id">
        insert into tableA (id, col1, col2)
        values ( tableA_id.nextval, #{col1}, #{col2})
</insert>

10.僅針對插入、更新腳本: mysql的&和oracle的||符號

insert或者update語句中有字段包含”&“符號時,需要用||連接符或者:

mysql:
字符:"abc&def"

oracle:

字符:'abc'||'&'||'def'

11. 僅針對插入、更新腳本:mysql的轉義字符 \ 和oracle的 ’

mysql:
字符:{text:\'this is a test\',value:\'nothing\'}
oracle:
字符:{text:''this is a test'',value:''nothing''}

12. 字段大小寫

oracle:
-- Oracle默認自動轉大寫,結果爲tableA中的id、Id、iD、ID字段對應值;
select  id  from tableA;
-- 可以理解成:單引號標識爲字符id,結果爲tableA中的id字段對應值;
select 'id' from tableA;
-- 同date等特殊字段,結果爲tableA中的id、Id、iD、ID字段對應值;
select "id" from tableA;
-- 結果爲tableA中的id、Id、iD、ID字段對應值;
select  ID  from tableA;
-- 結果爲tableA中的ID字段對應值;
select 'ID' from tableA;
-- 結果爲tableA中的id、Id、iD、ID字段對應值;
select "ID" from tableA;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章