Oracle到PostgreSQL数据库的语法迁移手册(建议收藏)

这篇具有很好参考价值的文章主要介绍了Oracle到PostgreSQL数据库的语法迁移手册(建议收藏)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

概述

异构数据库的迁移(譬如从Oracle迁移到PostgreSQL)工作主要包括三个方面,

  • 数据库对象的迁移,包括库、模式、表、索引、视图、触发器、存储过程等等;
  • 数据内容的迁移,主要指的是数据表中的数据;
  • 数据应用的迁移,主要指的是应用中SQL语句的迁移。

目前对于数据库对象以及数据内容的迁移有很多成熟的工具,而对于应用迁移的工具却很少能够见到。原因是因为DML语句比DDL复杂的多,不同的数据库语法差异也比较大。目前市场上的迁移工具大多使用正则表达式来解析SQL语句,而DML语句的复杂性导致此类工具的解析成功率较低,难以作为一个成熟地商业产品进行推广。

PawSQL团队开发的DML语法转换工具Ora2pg,通过PawSQL强大的SQLParser,能够解析几乎所有的Oracle语法,并将其转换为对应的PostgreSQL语法,支持数据库应用的平滑迁移。

本手册介绍了Oracle和PostgreSQL的语法区别,以及转换映射关系,可以作为迁移人员的SQL迁移参考手册。

本手册描述了PawSQL Ora2pg内部的实现逻辑,PawSQL Ora2pg能够帮助SQL迁移人员自动识别不兼容的语法,并完成语法转换。

虚拟表(dual)

虚拟表dual

Oracle获取一个常量需要通过一个dual,PostgreSQL不需要

编号 Oracle PostgreSQL
1 select 2 from dual select 2

虚拟列

虚拟列rownum

对于查询返回的每行数据,rownum虚拟列会返回一个数字,第一行的ROWNUM为1,第二行为2,以此类推。

  • rownum在select列表中时重写为row_number() over ()
  • rownum在where子句中时重写为limit… offset…
    | 编号 | Oracle | PostgreSQL |
    | ---- | ------------------------------------------------------------ | ------------------------------------------------------------ |
    | 1 | select rownum from customer; | select row_number() over () as rownum from customer |
    | 2 | select tableoid from customer where rownum < 10 and rownum >= 2; | select tableoid from customer limit 9 OFFSET 2 |
    | 3 | select c_name from customer where rownum < 10 and c_phone = ‘111’; | select customer.c_name from customer where customer.c_phone = ‘111’ limit 9 |
    | 4 | select * from customer where rownum between 1 and 10; | select tableoid from customer limit 10 OFFSET 0 |

虚拟列rowid

Oracle中的rowid虚拟列返回特定行的具体地址,在PostgreSQL中重写为tableoid || '#' || ctid

编号 Oracle PostgreSQL
1 select rowid, c.* from customer c; select tableoid || ‘#’ || ctid, c.* from customer as c

字符串函数

nvl(col, value)

Oracle中的nvl(col, value)用来设置默认值,col为空就设置为value;

在PostgreSQL中重写为coalesce

编号 Oracle PostgreSQL
1 select nvl(c_phone, 1) from customer; select coalesce(customer.c_phone, ‘1’) from customer

nvl2(col, v1, v2)

nvl2对col的null值进行处理,如果col为null,则返回v1, 否则返回v2;

postgre中没有类似的函数,可以重写为case… when…

编号 Oracle PostgreSQL
1 select nvl2(c_phone, 1, 2) from customer; select case when c_phone is null then 1 else 2 end from customer

decode(arg1, arg2, arg3, arg4)

Oracle中的decode(arg1, arg2, arg3, arg4)函数, 表示当 arg1 等于 arg2 时,取 arg3,否则取 arg4。

postgre中没有类似的函数,可以重写为case… when…

编号 Oracle PostgreSQL
1 select decode(c_phone,‘110’, 1 , 2) from customer; select case when c_phone = ‘110’ then 1 else 2 end from customer
2 select decode(c_phone,null, 1 , 2) from customer; select case when c_phone is null then 1 else 2 end from customer

substr(str, int, int)

Oracle中的substr用来取一个字符串的子串,PostgreSQL有同名的函数实现类似功能。不同的是Oracle中,第二、第三个参数可以为负数,代表从后面进行计数,PostgreSQL不允许其为负数,需对其进行转换。Oracle中是以0开始计数,PostgreSQL以1开始计数(需确认)。

编号 Oracle PostgreSQL
1 select substr(c_phone, 1 , -2 ) from customer; select substr(c_phone, 1, length(c_phone) - 2) from customer
2 select substr(c_phone, -3 , 1 ) from customer; select substr(c_phone, length(c_phone) - 3, 1) from customer

instr(str1, str2)

Oracle中的instr用来取一个字符串的子串位置,当其只有两个参数时,表示子串的第一次出现的位置,和PostgreSQL中对应的函数为strpos。当其有多个参数时,无对应函数。

编号 Oracle PostgreSQL
1 select instr(‘123’, ‘23’) select strpos(‘123’, ‘23’)

replace(srcstr, oldsub[, newsub ])

在Oracle中,replace()函数用于替换字符串, replace(srcstr, oldsub[, newsub ] ),和PostgreSQL中的replace函数用法基本一致。只是需要注意在Oracle中无第三个参数时,代表删除此字符,在PostgreSQL可将第三个参数设置为’'。

编号 Oracle PostgreSQL
1 select replace(‘123’,‘1’); select replace(‘123’,‘1’,‘’);

stragg(str,[str])

Oracle里的stragg函数实现在分组内对列值的拼接,它和listagg类似,但是不可以指定拼接的顺序。在PostgreSQL中,可以使用string_agg函数来替换。其第二个参数可选,默认值为’',在PostgreSQL需补充第二个参数。

编号 Oracle PostgreSQL
1 select listagg(c_name,‘,’) as name from customer group by c_phone select string_agg(c_name,‘,’) as name from customer group by c_phone
2 select listagg(c_name) as name from customer group by c_phone select listagg(c_name,‘’) as name from customer group by c_phone

listagg(str, [str])

Oracle里的listagg函数实现对列值的拼接,它可以在分组内以指定顺序对非分组列进行拼接。在PostgreSQL中,可以使用string_agg函数来实现,需注意语法方面也有区别. 另外,其第二个参数可选,默认值为’',在PostgreSQL需补充第二个参数。

  • 当没有group by子句时,可以使用over(partiton by… order by…)进行替换

  • 当指定group by子句时,它的重写算法比较复杂

    • 如果需要保持拼接的顺序,需要通过子查询来实现(见编号2)
    • 如果不需要保持拼接顺序,可以把它转化为简单的聚集函数(编号3)
编号 Oracle PostgreSQL
1 select listagg(c_name,‘,’) within group(order by c_name) over (partition by c_phone) as name from customer; sselect string_agg(customer.c_name, ‘,’) over (partition by customer.c_phone order by c_custkey) as name from customer
2 select listagg(c_name,‘,’) within group(order by c_name) as name from customer group by c_phone; select max(paw_dt.name) as name from (select string_agg(customer.c_name, ‘,’) over (partition by customer.c_phone order by c_name) as name,
customer.c_phone
from customer) as paw_dt
group by c_phone
3 select listagg(c_name,‘,’) within group(order by c_name) as name from customer group by c_phone select string_agg(c_name,‘,’) as name from customer group by c_phone

日期函数

sysdate/systimestamp

Oracle中的sysdate()/sysdate返回系统当前时间(日期+时分秒),在PostgreSQL中对应now()或是current_timestamp(日期+时分秒+毫秒)。

Oracle中的systimestamp返回系统当前时间戳(日期+时分秒+毫秒),在PostgreSQL中对应now()或是current_timestamp。

编号 Oracle PostgreSQL
1 select sysdate select current_timestamp
2 select sysdate() select now()
3 select systimestamp select current_timestamp

to_date(str, fmt)

Oracle中的to_date返回的是时间类型,而在PostgreSQL中to_date是日期类型,所以Oracle中的to_date在PostgreSQL中应该对应to_timestamp

编号 Oracle PostgreSQL
1 select to_date( endTime ,‘yyyy-mm-dd hh24:mi:ss’) from t select to_timestamp( endTime ,‘yyyy-mm-dd hh24:mi:ss’) from t

trunc(arg1, [arg2])

在Oracle中trunc函数有两种用法

  • 第一种是对数字进行截取, trunc(num,[int]); 是去掉数字num小数位以后的部分,并且不进行四舍五入。这种用法和在PostgreSQL的trunc用法一致,不需要转换

  • trunc函数的第二种用法是对日期进行提取,trunc(date,[fmt])。这种用法在PostgreSQL对应的函数是date_trunc(fmt, date),需注意在PostgreSQL中fmt是第一个参数,且不可省略。

编号 Oracle PostgreSQL
1 select trunc( 111.23,2) select trunc( 111.23,2)
2 select trunc(sysdate,‘year’) select date_trunc(‘year’, current_timestamp)
3 select trunc(sysdate) select date_trunc(‘dd’, current_timestamp)

add_months(date, int)

Oracle中的add_months 函数主要是对日期函数进行操作,对日期按月增加。在PostgreSQL没有对应的函数,需将其转化为基于日期和interval的运算。

编号 Oracle PostgreSQL
1 select add_months(sysdate, 2) select current_timestamp + 2 * interval ‘1 month’

last_day(date)

Oracle中的last_day返回指定日期所在月份的最后一天; 在PostgreSQL没有对应的函数,需将其转化为基于日期和interval的运算。

编号 Oracle PostgreSQL
1 select add_months(sysdate, 2) select cast(date_trunc(‘MONTH’, current_timestamp) + interval ‘1 MONTH - 1 DAY’ as date)

SQL语句

HAVING子句顺序

Oracle允许HAVING在GROUP BY子句之前或之后。在PostgreSQL中,HAVING子句必须出现在GROUP BY子句后面。

编号 Oracle PostgreSQL
1 select c_name from customer having count(*) > 2 group by c_name select c_name from customer group by c_name having count(*) > 2

括号中的表名

Oracle中单表引用允许使用括号括起来,PostgreSQL不允许。

编号 Oracle PostgreSQL
1 SELECT * FROM (CUSTOMER); SELECT * FROM CUSTOMER;

UNIQUE关键字

Oracle中允许使用UNIQUE进行去重,在PostgreSQL中迁移为DISTINCT关键字

编号 Oracle PostgreSQL
1 select unique c_phone from customer select distinct customer.c_phone from customer

MINUS关键字

Oracle中可以使用minus关键字来取两个结果集的差,在PostgreSQL中需迁移为except.

编号 Oracle PostgreSQL
1 select c_custkey from customer minus select o_custkey from orders select c_custkey from customer except select o_custkey from orders

FROM关键字

Oracle的delete语句的FROM关键字可以省略,迁移至PostgreSQL需补充上。

编号 Oracle PostgreSQL
1 delete customer where 1=0; delete from customer where 1 = 0

NOLOGGING关键字

Oracle在执行INSERT语句时,可以通过指定NOLOGGING关键字来减少日志记录,提升操作性能。PostgreSQL不支持此关键字。

编号 Oracle PostgreSQL
1 insert into customer nologging select * from customer_bk; insert into customer select * from customer_bk;

AS关键字

INSERT INTO 后面不需要添加as关键字,insert into ... as select... 修改为insert into... select...

编号 Oracle PostgreSQL
1 insert into t as select c1 from t1 insert into t select c1 from t1

FROM子查询的别名

Oracle中在不引起歧义的情况下子查询可以不带别名,而在PostgreSQL中,所有的FROM子查询都必须带有别名

编号 Oracle PostgreSQL
1 select * from (select * from CUSTOMER) select * from (select * from CUSTOMER) as foo

UPDATE语句里的字段名

在PostgreSQL中,Update的时候,更新列不允许添加表名前缀。

编号 Oracle PostgreSQL
1 update customer c set c.c_name = ‘xxx’ where c_custkey = 1; update customer set c_name = ‘xxx’ where c_custkey = 1

左(右)外连接

在Oracle中,外连接可以通过在条件上添加(+)来定义, 连接符(+)跟在哪个条件后面就是哪张表被左连。在PostgreSQL中,需将其重写为标准的外连接语法。

编号 Oracle PostgreSQL
1 select * from customer, orders where c_custkey = o_custkey(+) select * from customer left outer join orders on c_custkey = o_custkey
2 select * from customer, orders where c_custkey(+) = o_custkey and c_name(+) = o.o_clerk and o_custkey>100 select * fromcustomer right outer join orders on (c_custkey = o_custkey and c_name = o_clerk) where o_custkey > 100

CONNECT BY子句

Oracle中,CONNECT BY 用于存在上下级等层级关系的数据表进行递归查询。语法格式: START WITH condition1 CONNECT BY [ NOCYCLE ] condition2。在PostgreSQL通过Recursive Common Table Expression来实现此功能,主要是把START WITH… CONNECT BY Prior拆成两个部分,查询表一致,但条件不一致,用UNION ALL合并.

编号 Oracle PostgreSQL
1 select id from city_branch start with id=1 connect by prior id=parent_id; with RECURSIVE MIG_CTE as (
select id, 1 as level from city_branch where id = 1
union all
select id, level + 1 from city_branch, MIG_CTE where MIG_CTE.id = parent_id)
select * from MIG_CTE
2 select t.branch_level, t.id from city_branch c where (c.branch_level = ‘1’ or t.branch_level = ‘2’) and (t.sign = ‘1’ or t.sign = ‘4’ or t.sign = ‘8’) and t.status = ‘1’ start with c.id = 1 connect by c.id = prior c.parent_id order by c.branch_level desc with RECURSIVE MIG_CTE as
(select t.branch_level, t.id, 1 as level from city_branch as cwhere ((((branch_level = ‘1’ or t.branch_level = ‘2’)and ((t.sign = ‘1’ or t.sign = ‘4’) or t.sign = ‘8’)) and t.status = ‘1’) and c.id = 1)
union all
select t.branch_level, t.id, level + 1 from city_branch as c, MIG_CTE where ((((branch_level = ‘1’ or t.branch_level = ‘2’) and ((t.sign = ‘1’ or t.sign = ‘4’) or t.sign = ‘8’)) and t.status = ‘1’) and c.id = MIG_CTE.parent_id))
select * from MIG_CTE order by MIG_CTE.branch_level desc

操作符的强类型限制

Oracle中不同类型进行基于操作符的运算,会自动转化类型,譬如select 1 + '1' from dual。PostgreSQL是强类型,不同类型的运算会提示类型不匹配,执行select 1 + '1'会报错,需要进行显式的类型转换。

涉及的操作符类型包括:

操作符 操作符名称
+ 加法
- 减法
/ 除法
% 取余
* 乘法
|| 字符串拼接

数值运算(+,-,*,/,%)

编号 Oracle PostgreSQL
1 select 1 + ‘1’ select 1 + 1
2 select 1 + charCol from tbl select 1 + cast(charCol as numeric) from tbl
3 select ‘1’ - 1 select 1- 1
4 select 1 * charCol from tbl select 1 * **cast(charCol as numeric) ** from tbl
5 select 1 / charCol from tbl select 1 /cast(charCol as numeric) from tbl
6 select charCol % 2 from tbl select cast(charCol as numeric) % 2 from tbl

日期计算(+,-)

编号 Oracle PostgreSQL
1 select sysdate - 1 select current_timestamp - interval ‘1’ DAY
2 select 1 + sysdate() select interval ‘1’ DAY + now()
3 select systimestamp +1 select current_timestamp + interval ‘1’ DAY
4 select systimestamp - 1 select current_timestamp - interval ‘1’ DAY

字符串拼接(||)

编号 Oracle PostgreSQL
1 select 1||1 select ‘1’||‘1’
2 select 1 || c_custkey select 1 || cast(c_custkey as text)

函数参数的强类型限制

Oracle中在函数调用时,参数类型进行会自动转化类型,譬如 select substr(123.12,0,2)是合法的,且返回123。PostgreSQL是强类型, 执行select substr(123.12,0,2)会报错,需要进行显式的类型转换。

substr(arg1, arg2, arg3)

编号 Oracle PostgreSQL
1 select substr(1234.1, 0, 4) select substr(‘1234.1’, 1, 4+1)
2 select substr(‘1234.1’, 0, ‘2’) select substr(‘1234.1’, 0, 2)

sum(arg)

编号 Oracle PostgreSQL
1 select sum(‘2’) select sum(2)

avg(arg)

编号 Oracle PostgreSQL
1 select avg(‘2’) select avg(2)

round(arg)

编号 Oracle PostgreSQL
1 select round(‘2’) select round(2)

条件判断中的强类型限制

Oracle中在进行条件判断时,左右表达式的类型进行会自动转化,譬如 where c_phone = 110是合法的。PostgreSQL是强类型, 执行where c_phone = 110会报错,需要进行显式的类型转换。

比较运算(=、>、<、>=、<=、<>)

转换原则,优先转换常量类型;当两个都为数据列时,优先转换左边的。

编号 Oracle PostgreSQL
1 select * from customer where c_phone = 110 select * from customer where c_phone = ‘110’
2 select * from customer where ‘1’ = c_custkey select * from customer where 1 = c_custkey
3 select * from customer where c_phone = c_custkey select * from customer where cast(c_phone as int) = c_custkey

BETWEEN

转换原则,转换 var0 between var1 and var2 中的 var1, var2。

编号 Oracle PostgreSQL
1 select * from customer where c_custkey between ‘100’ and ‘200’; select * from customer where c_custkey between 100 and 200

IN LIST

转换原则,转换List中的变量。

编号 Oracle PostgreSQL
1 select * from customer where c_phone in (110,120); select * from customer where c_phone in (‘110’, ‘120’)

默认参数

Oracle中有部分函数存在默认参数,而在PostgreSQL其参数是必填项。

to_char(unknown)

编号 Oracle PostgreSQL
1 select to_char(c_custkey) from customer select cast(c_custkey as text) from customer

to_number(str)

编号 Oracle PostgreSQL
1 select to_number(‘100’) select 100
2 select to_number(c_phone) from customer; select cast(c_phone as numeric) from customer

PawSQL往期文章精选

从SQL质量管理体系来看SQL审核(3)- SQL开发规范

从SQL质量管理体系来看SQL审核(2)- SQL质量标准

高级SQL优化 | 你真的了解用UNION替换OR吗?

EverSQL向左,PawSQL向右

关于PawSQL

PawSQL专注数据库性能优化的自动化和智能化,支持MySQL,PostgreSQL,Opengauss等,提供的SQL优化产品包括:

PawSQL Cloud,在线自动化SQL优化工具,支持SQL审查,智能查询重写、基于代价的索引推荐,适用于数据库管理员及数据应用开发人员,
PawSQL Advisor,IntelliJ 插件, 适用于数据应用开发人员,可以IDEA/DataGrip应用市场通过名称搜索“PawSQL Advisor”安装。文章来源地址https://www.toymoban.com/news/detail-844224.html

到了这里,关于Oracle到PostgreSQL数据库的语法迁移手册(建议收藏)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【数据库迁移系列】从Oracle迁移到openGauss实战分享

    之前的迁移系列中我们介绍了Mysql到openGauss的迁移方法,本篇介绍使用Ora2og工具从Oracle到openGauss数据库的迁移。 ora2pg 可以将 Oracle 或者 MySQL 数据库迁移到 PostgreSQL,应用场景小到 Oracle 数据库的反向工程,大到大型企业数据库迁移,或者简单地将一些 Oracle 数据复制到 Postgre

    2023年04月08日
    浏览(38)
  • 从 sqlite 迁移到 Oracle 数据库

    今天发现一个有意思的竞赛,竞赛中使用了 sqlite 数据库。 由于个人更习惯 Oracle 数据库,所以将 sqlite 数据库迁移到了 Oracle 数据库。 此文章记录一下迁移时使用的 Python 代码。 完结!

    2024年04月29日
    浏览(22)
  • 从 Oracle 到 MySQL 数据库的迁移之旅

    目录 引言 一、前期准备工作 1.搭建新的MySQL数据库 2 .建立相应的数据表 2.1 数据库兼容性分析 2.1.1 字段类型兼容性分析 2.1.2 函数兼容性分析 2.1.3 是否使用存储过程?存储过程的个数?复杂度? 2.1.4 是否使用触发器?个数?使用的场景? 2.2 建表过程中其他需要注意的事项

    2024年04月11日
    浏览(36)
  • 从MySQL迁移到Oracle数据库的详细步骤和方法

    在一些情况下,可能需要将现有的MySQL数据库迁移到Oracle数据库。本文将详细介绍如何进行这一迁移过程,涵盖了备份、转换和导入等关键步骤,以帮助你顺利完成数据库迁移。 在开始迁移之前,务必备份你的MySQL数据库,以便在出现问题时可以还原数据。 确保你已经安装了

    2024年02月08日
    浏览(37)
  • 数据库信息速递 AWS因迁移PostgreSQL DBaaS而遭遇长时间停机时间而备受诟病

    开头还是介绍一下群,如果感兴趣polardb ,mongodb ,mysql ,postgresql ,redis 等有问题,有需求都可以加群群内有各大数据库行业大咖,CTO,可以解决你的问题。加群请联系 liuaustin3 ,在新加的朋友会分到2群(共840人左右 1 + 2 + 3)新人会进入3群。 亚马逊的云业务告诉用户,它将在2

    2024年02月09日
    浏览(62)
  • 【数据库】如何利用Python中的petl将PostgreSQL中所有表的外键删除,迁移数据,再重建外键

    在数据库管理中,外键是一种重要的约束,用于确保数据的一致性和完整性。然而,在某些情况下,我们可能需要删除或修改外键。本文将介绍如何使用Python中的petl库将PostgreSQL中所有表的外键删除,迁移数据,并重新建立外键。 首先,我们需要安装petl和psycopg2库。在命令行

    2024年02月10日
    浏览(34)
  • 实例讲解C++连接各种数据库,包含SQL Server、MySQL、Oracle、ACCESS、SQLite 和 PostgreSQL、MongoDB 数据库

      C++ 是一种通用的编程语言,可以使用不同的库和驱动程序来连接各种数据库。以下是一些示例代码,演示如何使用 C++ 连接 SQL Server、MySQL、Oracle、ACCESS、SQLite 和 PostgreSQL、MongoDB 数据库。 连接 SQL Server 数据库 要使用 C++ 连接 SQL Server 数据库,可以使用 Microsoft 的 ADODB 库。以

    2024年02月05日
    浏览(46)
  • Oracle、MySQL数据库常规命令语法-简易记录(非常规持续更新)

    前言:呈现的是非常基础必备命令以及常规关联语法,因涉及到不同数据库其表达都会有所区别,此篇纯属做个仓库记录更非常规持续更新,专业人士可忽略,且看且珍惜… MySQL: 关系型数据库、重点开源、支持大型规模、标准SQL数据语言、多平台多架构、高可用集群、可定

    2024年01月25日
    浏览(42)
  • Sqlserver_Oracle_Mysql_Postgresql不同关系型数据库之主从延迟的理解和实验

    关系型数据库主从节点的延迟是否和隔离级别有关联,个人认为两者没有直接关系,主从延迟在关系型数据库中一般和这两个时间有关:事务日志从主节点传输到从节点的时间+事务日志在从节点的应用时间 事务日志从主节点传输到从节点的时间,相关因素有以下2点: 1、事

    2024年02月14日
    浏览(41)
  • [运维|数据库] 将mysql的null.unix_timestamp(now()) * 1000转为PostgreSQL的语法

    在 PostgreSQL 中,您可以使用以下方式将 MySQL 中的 UNIX_TIMESTAMP 和 NOW() 函数的组合转换为等效的语法: 在这个 PostgreSQL 表达式中: EXTRACT(EPOCH FROM NOW()) 获取当前时间戳的秒数。 2. * 1000 将秒数转换为毫秒。

    2024年02月07日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包