日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

pg和oracle比较,Oracle与PostgreSQL使用差异对比与总结

發布時間:2023/12/3 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pg和oracle比较,Oracle与PostgreSQL使用差异对比与总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

1、基本數據類型差異

Oracle

PostgreSQL

Varchar2

varchar

number

numeric

date

timestamp/date/time

不支持boolean,可通過0/1代替

支持boolean

null

null

2、基本函數差異

item

Oracle

PostgreSQL

系統當前時間

SYSDATE

now()/CURRENT_TIMESTAMP/CURRENT_DATE/CURRENT_TIME

對時間或數字截取

trunc()

date_trunc()

to_char,to_number,

to_date

自動格式轉換

需指定日期格式

eg:to_date(timejoin,'yyyy-MM-dd')

判空操作

nvl()

coalesce()

條件判斷

decode()

case...when...then

dual偽表

支持

不支持(查詢常量不需要加from)

其他用法一致的常用函數:

mod(n2,n1) -- n2除n1取余數;? ? ? ? ? sign(n) -- 判斷n的符號;

floor(n) -- 取小于等于n的正整數;? ? ?ceil() -- 取大于等于n的正整數;

round(n,integer) -- 對n四舍五入,保留位數為integer;? ?trunc(n,integer) -- 對n截取,截取保留的位數為integer;

covert(char,dest_sest,source_set) -- 字符集轉換,例:convert(username, 'ZHS16GBK','UTF8');

cast(expr as type_name) -- 數據類型轉換,常用于數字與字符間轉換,例:cast(id_no as varchar);

部分函數的使用簡析:

(1)coalesce(COL1,COL2,COL3):返回參數中第一個非null字段值

例如:coalesce(COL1,0):如果COL1為null或‘’,則返回默認值0;否則返回COL1的值;

(2)extract(date):對日期特定部分提取(oracle和postgresql使用一致)

例如:extract(year from now());>>>2018

extract(month from now());>>>9

extract(month from timestamp '2018-09-10 13:59:59');>>>9

(3)對時間截取trunc()和date_trunc()

>>oracle--trunc()的用法:

trunc(sysdate,'yyyy');//返回當前年的第一天>>>2018-01-01

trunc(sysdate, 'mm');//返回當前月的第一天>>>2018-09-01

trunc(sysdate, 'dd');//返回當前時間的年月日>>>2018-09-14

trunc(sysdate, 'hh');//返回當前小時>>>2018-09-14 13:30:50

>>postgreSQL--date_trunc()用法:

date_trunc('year',now());//返回當前時間年的第一天>>>2018-01-01 00:00:00

date_trunc('month',now());//返回當前月的第一天>>2018-09-01 00:00:00

date_trunc('day',now()); //返回當前時間的年月日>>2018-09-14 00:00:00

date_trunc('second',now()); //返回當前時間的年月日時分秒>>2018-09-14 13:30:50

(3)條件判斷

Oracle:

Select DECODE (payments_info,'CR','Credit','DB','Debit', null) FROM dual;

PostgreSQL:

Select CASE

WHEN foo = 'CR' ? THEN 'Credit'

WHEN foo = 'DB' ? THEN 'Debit'

ELSE 'default'

END

FROM t2;

3、DDL語法差異

oracle和pgSQL操作表結構語法基本一致:

修改表名:alter table tbl_user rename tbl_user2;

添加約束:alter table 表名 add constraint 表名_列名_nn check (is not null)

添加列:alter table tbl_user add age number(3) default 18 not null;

alter table tbl_user add age number(3) default 18 not null after sex;(在指定列后添加列)

刪除列:alter table tbl_user drop column age;

修改列:alter table tbl_user modify password default'000000' not null;(修改約束)

修改列名:alter table tbl_user rename column password to pwd;

只有更改列的數據類型寫法有些差異

Oracle:ALTER?TABLE?table_name?modify column_name?datatype;

PostgreSQL:ALTER TABLE table_name ALTER column_name TYPE datatype;

4、DML語法差異

oracle和pgSQL增刪改查語法基本一致,只有upsert有差異

Oracle:有自帶的merge into功能(一個強大的操作)

PostgreSQL:不支持merge操作,可以使用on conflict() do

例:insert into TargetTable

select id,desc

from SourceTable

on conflict (id)

do update set

desc = exclude.desc

5、查詢語句差異

(1)查詢表中最新n條數據(Oracle有rownum,postgreSQL有limit)

postgreSQL:

select * from olc.olc_member_intebid_info order by create_time desc?limit?n;

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

Oracle:

寫法一:select t.* from (select * from nwd.tc_inte_bid_record order by create_time desc) t?whererownum?<= n;

寫法二:select * from(select t.*,?row_number() over(order by create_time desc)?rn from nwd.tc_inte_bid_record t) where rn <=n;

上述寫法一為通用常規寫法;寫法二可以對分組后數據排序,分組語句寫在over()中

(2)子查詢

postgresql子查詢要求比較嚴格,必須具有別名才可以

6、 Postgresql命令行常用操作(psql)

psql -d dbname -U username -p 5210 -h 172.0.0.1

--password 's&cws123'

如果不想輸入密碼,可以在.pgpass隱藏文件中添加密碼,格式:

172.0.0.1:5210:dbname:username:password

注意.pgpass的權限問題:chmod 0600 ~/.pgpass

-- 查詢某個庫下的所有表(\dt)

select * from pg_tables where schemaname = 'idn_dw';

-- 查詢某個存儲過程(\df)

select proname,prosrc from pg_proc where proname = 'func_dwd_customer_info';

-- 查詢某個表下的字段(\d tablen_ame)

select table_schema,table_name,t.colname,string_agg(column_name,',') as COLS

from information_schema.columns

LEFT JOIN (select pg_class.relname as tablename,pg_attribute.attname as colname from

pg_constraint inner join pg_class

on pg_constraint.conrelid = pg_class.oid

inner join pg_attribute on pg_attribute.attrelid = pg_class.oid

and pg_attribute.attnum = pg_constraint.conkey[1]

where pg_constraint.contype='p') t

on table_name=t.tablename

where TABLE_NAME = 's10_anfd_rule'

group by table_schema,table_name,t.colname;

總結

以上是生活随笔為你收集整理的pg和oracle比较,Oracle与PostgreSQL使用差异对比与总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。