PostgreSQLとOracleのsql差异 [数据库教程]
PostgreSQLとOracleのsql差异
1.rownum
(1)Oracle分页查询使用rownum,PostgreSQL使用limit offset
POSTGRESQL
select * from (select rownum r,e.* from emp e where rownum <=5) t where r>0;
select * from emp limit 5 offset 0;
(2)Oracle中rownum=1,PostgreSQL中使用limit 1
ORACLE POSTGRESQL
select * from emp where rownum = 1;
select * from emp limit 1;
(3)Oracle中序号列rownum,PostgreSQL使用窗口函数
ORACLE POSTGRESQL
select rownum,t.* from emp t;
select row_number() over(), t.* from emp t;
2.系统日期
ORACLE POSTGRESQL
SYSDATE
current_timestamp, current_date
3.delete语句
Oracle delete语句可以没有from,pg必须要有from
ORACLE POSTGRESQL
delete from emp where empno = xxx; delete emp where empno = xxx
delete from emp where empno = xxx
4.类型自动转换
Oracle支持类型自动转换,例如数字自动换换为字符串等;PG中需要显示转换,或者添加CAST
5.子查询别名
PostgreSQL在from关键字后的子查询必须要有别名,Oralce可以没有。
6. group by having
PG having语句必须在group by之后,oracle可以在group by之前
7.递归查询
Oracle中使用start with … connect by…, PG中使用with recusive
ORACLE POSTGRESQL
select * from larearrelation where rearedgens = 1 and rearflag = 1 and rearlevel = ‘01’ connect by prior agentcode = rearagentcode start with rearagentcode = ‘10032226’;
with recursive rs as ( select * from larearrelation where rearagentcode = ‘10032226’ union all select a. *from larearrelation a, rs where a.rearagentcode = rs.agentcode ) select ** from rs where rearedgens = 1 and rearflag = ‘1’ and rearlevel = ‘01’
8.update语句别名
postgresql中update语句时,set的字段不能有别名
ORACLE POSTGRESQL
update emp t set t.name = ‘xxx’ where t.empno = 2
update emp set name = ‘xxx’ where empno = 2
9. 日期相减
oracle日期相减自动转换为数字,结果为相差的天数。pg日期相减为interval类型,得到相差天数需要进行类型转换
10.递归查询中的level
oracle的递归查询中level表示查询深度(或者递归层次),在PG中没有此含义的关键字,需要自行在with recursive实现
ORACLE POSTGRESQL
select max(level) from larearrelation where rearedgens = 1 and rearflag = 1 and rearlevel = ‘01’ connect by prior agentcode = rearagentcode start with rearagentcode = ‘10032226’;
with recursive rs as ( select larearrelation.*, 1 depth from larearrelation where rearagentcode = ‘10032226’ union all select a./**, rs.depth + 1 depth from larearrelation a, rs where a.rearagentcode = rs.agentcode ) select max(rs.depth) from rs where rearedgens = 1 and rearflag = ‘1’ and rearlevel = ‘01’
11.序列的调用
ORACLE POSTGRESQL
select seqname.nextval from dual;
select nextval(‘seqname’)
12.外连接
Oralce外连接支持使用 (+), PostgreSQL需使用left jion或者right join标准sql语法
13.distinct去重复
oracle支持unique关键字去重复,pg中只能使用distinct
14.字符串分割
ORACLE POSTGRESQL
listagg
string_agg
15.集合相减
ORACLE POSTGRESQL
Minus
except
16.null与 ‘‘
null和’’在oracle中是一致的,最终都会存储为null,在PG中会进行区分
17.不等于
Oracle中 ! =,< >操作符中间允许有空格,PG中不可以
18.别名
PG中无效的别名,可以尝试加as关键字,例如name
19.正则表达式
ORACLE POSTGRESQL
SELECT REGEXP_SUBSTR(‘17,20,23’,’,+’,1,1,’i’) FROM DUAL;
select (regexp_matches(‘17,20,23’, ‘,+’))[1]
20.字段大小写
oracle字段名大写,PG字段名小写
PostgreSQLとOracleのsql差异
以上是 PostgreSQLとOracleのsql差异 [数据库教程] 的全部内容, 来源链接: utcz.com/z/535287.html