日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

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

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、基本數(shù)據(jù)類型差異

Oracle

PostgreSQL

Varchar2

varchar

number

numeric

date

timestamp/date/time

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

支持boolean

null

null

2、基本函數(shù)差異

item

Oracle

PostgreSQL

系統(tǒng)當(dāng)前時間

SYSDATE

now()/CURRENT_TIMESTAMP/CURRENT_DATE/CURRENT_TIME

對時間或數(shù)字截取

trunc()

date_trunc()

to_char,to_number,

to_date

自動格式轉(zhuǎn)換

需指定日期格式

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

判空操作

nvl()

coalesce()

條件判斷

decode()

case...when...then

dual偽表

支持

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

其他用法一致的常用函數(shù):

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

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

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

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

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

部分函數(shù)的使用簡析:

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

例如:coalesce(COL1,0):如果COL1為null或‘’,則返回默認(rèn)值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');//返回當(dāng)前年的第一天>>>2018-01-01

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

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

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

>>postgreSQL--date_trunc()用法:

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

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

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

date_trunc('second',now()); //返回當(dāng)前時間的年月日時分秒>>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操作表結(jié)構(gòu)語法基本一致:

修改表名: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;

只有更改列的數(shù)據(jù)類型寫法有些差異

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條數(shù)據(jù)(Oracle有rownum,postgreSQL有l(wèi)imit)

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;

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

(2)子查詢

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

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的權(quán)限問題: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;

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。