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 |
字符串链接 | || | concat() |
系统当前时间 | SYSDATE | now()/CURRENT_TIMESTAMP/CURRENT_DATE/CURRENT_TIME |
对时间或数字截取 | trunc() | trunc()/date_trunc() |
to_char,to_number, to_date |
自动格式转换 | 需指定日期格式 eg:to_date(timejoin,'yyyy-MM-dd') |
判空操作 | nvl() | coalesce() |
条件判断 | decode() | case...when...then |
去空格 | trim()/ltrim()/rtrim() | trim() |
字符串截取 | substr() | substr() |
dual伪表 | 支持 | 不支持 |
(1)coalesce(COL1,COL2,COL3):返回参数中第一个非null字段值
例如:coalesce(COL1,0):如果COL1为null或‘’,则返回默认值0;否则返回COL1的值;
(2)对时间或数据截取trunc
trunc(Date)的用法:
trunc(sysdate,'yyyy');//返回当前年的第一天
trunc(sysdate, 'mm');//返回当前月的第一天
trunc(sysdate, 'dd');//返回当前时间的年月日
trunc(sysdate, 'd');//返回当前星期的第一天
trunc(sysdate, 'hh');//返回当前小时
trunc(number)的用法:
trunc(55.5,-1) = 50;//-1(负数)表示从小数点左边第一位截取后面全置为零;
trunc(55.55,1) = 55.5;//1(正数)表示小数点后面保留一位;
trunc(55.55) = 55;//截取整数部分;
(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:ALTER TABLE table_name modify(column_name datatype);
PostgreSQL:ALTER TABLE table_name ALTER column_name TYPE datatype;
4、查询语句差异
(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 where rownum <= 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子查询要求比较严格,必须具有别名才可以