《卸甲笔记》-PostgreSQL和Oracle的SQL差异分析之一:外连接

简介:

PostgreSQL是世界上功能最强大的开源数据库,在国内得到了越来越多机构和开发者的青睐和应用。随着PostgreSQL的应用越来越广泛,Oracle向PostgreSQL数据库的数据迁移需求也越来越多。数据库之间数据迁移的时候,首先是迁移数据,然后就是SQL、存储过程、序列等程序中不同的数据库中数据的使用方式的转换。下面根据自己的理解和测试,写了一些SQL以及数据库对象转换方面的文章,不足之处,尚请多多指教。

外连接

Oracle数据库中,数据的外连接有两种写法。一种是标准SQL的写法。【left (outer) join XXX on】。这种标准写法Oracle和PostgreSQL都是支持的。但是,Oracle还有自己的一种写法,就是(+)。这种写法PostgreSQL不支持。所以数据迁移过来以后,需要将Oracle的这种特有的SQL写法转换成PostgreSQL支持的写法,程序才能够在PostgreSQL数据库中正常的运行。下面利用下面的表数据分析一下Oracle的这种外连接的写法和转换的方式。

SQL> select * from employ;

        ID NAME              AGE ADDR
---------- ---------- ---------- --------------------
         1 赵大               20 山东济南
         2 钱二               20 山东青岛
         4 李四               20 山东济南
         5 周五               20 山东日照

SQL> select * from salary;

     EMPID YYMM       SALARY
---------- ------ ----------
         1 200010       6000
         2 200010       5000
         3 200010       7000

SQL> select * from achieve;

     EMPID YYMM      ACHIEVE
---------- ------ ----------
         1 200010     150000
         2 200010     100000
         5 200011      50000

1、左外连接

Oracle的(+)写在等号右侧的时候,表示是以等号左边的表为主表,以右侧的表为连接表的左外连接。以左侧的表为主表,右侧的表的数据如果有,则把数据取出来显示。如果没有,则 显示空(NULL)。
PostgreSQL中,可以把它转化成对应的【left (outer) join on】语句就可以了。注意两个相同的表有多个关联条件的情况(比如下面的第二个SQL)。

Oracle 左外连接
SQL> select  emp.id,  emp.Name, sal.yymm, sal.salary
  2  from employ emp, salary sal
  3  where  emp.id = sal.empid(+);

        ID NAME       YYMM       SALARY
---------- ---------- ------ ----------
         1 赵大       200010       6000
         2 钱二       200010       5000
         5 周五
         4 李四

SQL> select ach.empid, ach.yymm, ach.achieve, sal.salary
  2  from  achieve ach,  salary sal
  3  where  ach.empid=sal.empid(+)
  4  and ach.yymm=sal.yymm(+);

     EMPID YYMM      ACHIEVE     SALARY
---------- ------ ---------- ----------
         1 200010     150000       6000
         2 200010     100000       5000
         5 200011      50000
对应的PostgreSQL的SQL
postgres=# select  emp.id,  emp.Name, sal.yymm, sal.salary
postgres-# from employ emp
postgres-# left outer join salary sal on emp.id=sal.empid;
 id | name |  yymm  | salary
----+------+--------+--------
  1 | 赵大 | 200010 |   6000
  2 | 钱二 | 200010 |   5000
  5 | 周五 |        |
  4 | 李四 |        |
(4 行记录)

postgres=# select ach.empid, ach.yymm, ach.achieve, sal.salary
postgres-# from  achieve ach
postgres-# left join salary sal
postgres-# on  ach.empid=sal.empid
postgres-# and ach.yymm=sal.yymm;
 empid |  yymm  | achieve | salary
-------+--------+---------+--------
     1 | 200010 |  150000 |   6000
     2 | 200010 |  100000 |   5000
     5 | 200011 |   50000 |
(3 行记录)

2、右外连接

Oracle的(+)写在等号左侧的时候,表示是以等号右边的表为主表,以左侧的表为连接表的右外连接。以右侧的表为主表,左侧的表的数据如果有,则把数据取出来显示。如果没有,则 显示空(NULL)。
PostgreSQL中,可以把它转化成对应的【right (outer) join on】语句就可以了。

Oracle 右外连接
SQL> select  emp.id,  emp.Name, sal.yymm, sal.salary
  2  from employ emp, salary sal
  3  where  emp.id(+) = sal.empid;

        ID NAME       YYMM       SALARY
---------- ---------- ------ ----------
         1 赵大       200010       6000
         2 钱二       200010       5000
                      200010       7000

SQL> select ach.empid, ach.yymm, ach.achieve, sal.salary
  2  from  achieve ach,  salary sal
  3  where  ach.empid(+)=sal.empid
  4  and ach.yymm(+)=sal.yymm;

     EMPID YYMM      ACHIEVE     SALARY
---------- ------ ---------- ----------
         1 200010     150000       6000
         2 200010     100000       5000
                                   7000
对应的PostgreSQL的SQL
postgres=# select  emp.id,  emp.Name, sal.yymm, sal.salary
postgres-# from employ emp
postgres-# right outer join salary sal on emp.id=sal.empid;
 id | name |  yymm  | salary
----+------+--------+--------
  1 | 赵大 | 200010 |   6000
  2 | 钱二 | 200010 |   5000
    |      | 200010 |   7000
(3 行记录)

postgres=# select ach.empid, ach.yymm, ach.achieve, sal.salary
postgres-# from  achieve ach
postgres-# right join salary sal
postgres-# on  ach.empid=sal.empid
postgres-# and ach.yymm=sal.yymm;
 empid |  yymm  | achieve | salary
-------+--------+---------+--------
     1 | 200010 |  150000 |   6000
     2 | 200010 |  100000 |   5000
       |        |         |   7000
(3 行记录)

3、多个连接条件的注意事项

Oracle中,两个表通过多个关连条件外连接的时候,如果多个条件中有没有写(+)的条件,则连接会自动变成内连接,而不再是外连接。这种情况应该是属于写SQL的失误。遇到这种情况的时候一定要注意。

Oracle
SQL> select ach.empid, ach.yymm, ach.achieve, sal.salary
  2  from  achieve ach,  salary sal
  3  where  ach.empid=sal.empid
  4  and ach.yymm=sal.yymm;

     EMPID YYMM      ACHIEVE     SALARY
---------- ------ ---------- ----------
         1 200010     150000       6000
         2 200010     100000       5000

SQL> select ach.empid, ach.yymm, ach.achieve, sal.salary
  2  from  achieve ach,  salary sal
  3  where  ach.empid=sal.empid(+)
  4  and ach.yymm=sal.yymm(+);

     EMPID YYMM      ACHIEVE     SALARY
---------- ------ ---------- ----------
         1 200010     150000       6000
         2 200010     100000       5000
         5 200011      50000

SQL> select ach.empid, ach.yymm, ach.achieve, sal.salary
  2  from  achieve ach,  salary sal
  3  where  ach.empid=sal.empid(+)
  4  and ach.yymm=sal.yymm;

     EMPID YYMM      ACHIEVE     SALARY
---------- ------ ---------- ----------
         1 200010     150000       6000
         2 200010     100000       5000
对应的PostgreSQL的SQL
postgres=# select ach.empid, ach.yymm, ach.achieve, sal.salary
postgres-# from  achieve ach
postgres-# left join salary sal
postgres-# on  ach.empid=sal.empid
postgres-# and ach.yymm=sal.yymm;
 empid |  yymm  | achieve | salary
-------+--------+---------+--------
     1 | 200010 |  150000 |   6000
     2 | 200010 |  100000 |   5000
     5 | 200011 |   50000 |
(3 行记录)

postgres=# select ach.empid, ach.yymm, ach.achieve, sal.salary
postgres-# from  achieve ach, salary sal
postgres-# where ach.empid=sal.empid
postgres-# and ach.yymm=sal.yymm;
 empid |  yymm  | achieve | salary
-------+--------+---------+--------
     1 | 200010 |  150000 |   6000
     2 | 200010 |  100000 |   5000
(2 行记录)
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
SQL 数据可视化 关系型数据库
MCP与PolarDB集成技术分析:降低SQL门槛与简化数据可视化流程的机制解析
阿里云PolarDB与MCP协议融合,打造“自然语言即分析”的新范式。通过云原生数据库与标准化AI接口协同,实现零代码、分钟级从数据到可视化洞察,打破技术壁垒,提升分析效率99%,推动企业数据能力普惠化。
1126 3
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
2116 152
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
1442 156
|
SQL 关系型数据库 MySQL
Go语言数据库编程:使用 `database/sql` 与 MySQL/PostgreSQL
Go语言通过`database/sql`标准库提供统一数据库操作接口,支持MySQL、PostgreSQL等多种数据库。本文介绍了驱动安装、连接数据库、基本增删改查操作、预处理语句、事务处理及错误管理等内容,涵盖实际开发中常用的技巧与注意事项,适合快速掌握Go语言数据库编程基础。
1906 213
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
关系型数据库 MySQL 索引
MySQL多表练习笔记
链接可行,多表查询语法
291 0
|
SQL 关系型数据库 PostgreSQL
CTE vs 子查询:深入拆解PostgreSQL复杂SQL的隐藏性能差异
本文深入探讨了PostgreSQL中CTE(公共表表达式)与子查询的选择对SQL性能的影响。通过分析两者底层机制,揭示CTE的物化特性及子查询的优化融合优势,并结合多场景案例对比执行效率。最终给出决策指南,帮助开发者根据数据量、引用次数和复杂度选择最优方案,同时提供高级优化技巧和版本演进建议,助力SQL性能调优。
|
存储 关系型数据库 测试技术
拯救海量数据:PostgreSQL分区表性能优化实战手册(附压测对比)
本文深入解析PostgreSQL分区表的核心原理与优化策略,涵盖性能痛点、实战案例及压测对比。首先阐述分区表作为继承表+路由规则的逻辑封装,分析分区裁剪失效、全局索引膨胀和VACUUM堆积三大性能杀手,并通过电商订单表崩溃事件说明旧分区维护的重要性。接着提出四维设计法优化分区策略,包括时间范围分区黄金法则与自动化维护体系。同时对比局部索引与全局索引性能,展示后者在特定场景下的优势。进一步探讨并行查询优化、冷热数据分层存储及故障复盘,解决分区锁竞争问题。
|
关系型数据库 分布式数据库 PolarDB
《阿里云产品手册2022-2023 版》——PolarDB for PostgreSQL
《阿里云产品手册2022-2023 版》——PolarDB for PostgreSQL
803 0
|
存储 缓存 关系型数据库

推荐镜像

更多