溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

postgresql和oracle數(shù)據庫有什么不同

發(fā)布時間:2020-08-01 11:17:49 來源:億速云 閱讀:2694 作者:清晨 欄目:編程語言

這篇文章主要介紹postgresql和oracle數(shù)據庫有什么不同,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

Postgresql與Oracle相關差異對比

通過查閱資料及實驗,以下對比了 Postgresql 9.3 版本與 Oracle 11g版本的相關差異。注意:相關細節(jié)仍待考證和完善。

1、基本語法差異

1.1 基本數(shù)據類型差異

postgresql和oracle數(shù)據庫有什么不同

1.2 基本函數(shù)差異

postgresql和oracle數(shù)據庫有什么不同

1.2.1 游標屬性

postgresql和oracle數(shù)據庫有什么不同

1.2.2 系統(tǒng)內置函數(shù)包

postgresql和oracle數(shù)據庫有什么不同

1.3 DDL差異

1.3.1 Sequence語法及使用差異

postgresql和oracle數(shù)據庫有什么不同

注意:pgsql中的dual,需自主實現(xiàn)。詳見兼容性設置->虛表dual問題 章節(jié)。

1.3.2 constraint差異

postgresql和oracle數(shù)據庫有什么不同

2、高級語法差異

2.1 事務差異

Oracle中,通過commit/rollback來實現(xiàn)事務提交或回滾。結構類似于:

begin
    select ...
    update ...
    delete ...
    commit;
exception
    when others then
    rollback;
end;

PostgreSQL實際上把每個SQL語句當做在一個事務中執(zhí)行來看待。 如果你沒有發(fā)出BEGIN命令,那么每個獨立的語句都被一個隱含的BEGIN和(如果成功的話)COMMIT包圍。一組包圍在BEGIN和COMMIT之間的語句有時候被稱做事務塊。

例如:

BEGIN;
UPDATE accounts SET balance = balance - 100.00
    WHERE name = 'Alice';
SAVEPOINT my_savepoint;
UPDATE accounts SET balance = balance + 100.00
    WHERE name = 'Bob';
-- 呀!加錯錢了,應該用 Wally 的賬號
ROLLBACK TO my_savepoint;
UPDATE accounts SET balance = balance + 100.00
    WHERE name = 'Wally';
COMMIT;

在PL/pgSQL中,也提供了類似于Oracle的Begin、End及ExceptionCode處理機制。他們之間的差異不明顯。事實上,PL/SQL(Oracle數(shù)據庫操作語言)與PL/pgSQL是高度相似的,這讓procedure在Oracle與 Postgresql之間遷移變得極為便捷。

2.2 函數(shù)繼承與重載

Oracle不支持繼承和重載特性,pgsql支持繼承和函數(shù)重載;

2.3 類型轉換

pgsql中的類型轉換“::”符,Oracle不支持。

2.4 子查詢

子查詢,pgsql要求更嚴格,必須具有別名才可以。

3、其他差異

3.1 jdbc差異

Oracle的jdbc連接字符串:db.url=jdbc:oracle:thin:@192.168.1.1:1521:ORCL

Postgresql的連接字符串:db.url=jdbc:postgresql:@192.168.1.1:5432/database

4、兼容性設置

4.1 字符串連接兼容性解決方案

Postgresql中沒有concat函數(shù),且由于||無法使用,需要通過在public schema中創(chuàng)建concat函數(shù)來解決。

--在 public schema中創(chuàng)建 concat 函數(shù)
create or replace function concat(text, text) returns text as
$body$ select coalesce($1,'') || coalesce($2,'')$body$ language 'sql' volatile;

alter function concat(text, text) owner to postgres;

4.2 虛表dual問題

Postgresql中沒有dual虛擬表,為保證兼容性,需創(chuàng)建偽視圖(view)代替:

create or replace view dual as
select NULL::"unknown"
where 1=1;

alter table dual owner to postgres;
grant all on table dual to postgres;
grant select on table dual to public;

4.3 數(shù)據分頁問題

Oracle中沒有 limit,postgresql中沒有rownum。需重寫相關語句。

-- Oracle
select * from (
  select * from (
    select * from t1 order by col1, col2
  ) where rownum <=50 order by col3, col4
) where rowmun <=20 order by col5, col6;
-- postgresql
select * from (select * from (select * from t1 order by col1, col2) ta order by col3, col4 limit 50) tb
order by col5, col6 limit 20;

注意:limit必須用于 order by 之后!

以上是postgresql和oracle數(shù)據庫有什么不同的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI