oracle中表的(+)左外连接,右外连接,全外连接

 

oracle中表的左外连接,右外连接,全外连接

 

左外连接的driver table是from后面的第一个表(外连接的表) 

右外连接的driver table是from后面的第二个表(外连接的表) 

 

 

create table employees(
last_name varchar2(20),
department_id number(6)
)
/

insert into employees(last_name,department_id)
values('eygle',1);
insert into employees(last_name,department_id)
values('bity_rainy',2);
insert into employees(last_name,department_id)
values('piner',3);
insert into employees(last_name,department_id)
values('coolyl',5);

create table departments(
department_id number(6),
department_name varchar2(20)
)
/
insert into departments(department_id,department_name)
values(1,'alibaba');
insert into departments(department_id,department_name)
values(2,'oracle');
insert into departments(department_id,department_name)
values(3,'ibm');
insert into departments(department_id,department_name)
values(4,'sun');


---1. 普通的相等连接
select e.last_name, d.department_name
from employees e, departments d
where e.department_id = d.department_id;

---2 左外连接 LEFT OUTER JOIN
select e.last_name,e.department_id,d.department_name
from employees e
left outer join departments d
on (e.department_id = d.department_id);

select e.last_name, d.department_name
from employees e, departments d
where e.department_id = d.department_id(+);
结果为:所有员工及对应部门的记录,包括没有对应部门编号department_id的员工记录。

 

SQL> set autotrace on
SQL> select e.last_name,e.department_id,d.department_name
  2  from employees e
  3  left outer join departments d
  4  on (e.department_id = d.department_id)
  5  /

LAST_NAME            DEPARTMENT_ID DEPARTMENT_NAME
-------------------- ------------- --------------------
eygle                            1 alibaba
bity_rainy                       2 oracle
piner                            3 ibm
coolyl                           5


执行计划
----------------------------------------------------------
Plan hash value: 2296652067

--------------------------------------------------------------------------------
--

| Id  | Operation          | Name        | Rows  | Bytes | Cost (%CPU)| Time
 |

--------------------------------------------------------------------------------
--

|   0 | SELECT STATEMENT   |             |     4 |   200 |     7  (15)| 00:00:01
 |

|*  1 |  HASH JOIN OUTER   |             |     4 |   200 |     7  (15)| 00:00:01
 |

|   2 |   TABLE ACCESS FULL| EMPLOYEES   |     4 |   100 |     3   (0)| 00:00:01
 |

|   3 |   TABLE ACCESS FULL| DEPARTMENTS |     4 |   100 |     3   (0)| 00:00:01
 |

--------------------------------------------------------------------------------
--


Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID"(+))

Note
-----
   - dynamic sampling used for this statement


统计信息
----------------------------------------------------------
          7  recursive calls
          0  db block gets
         31  consistent gets
          0  physical reads
          0  redo size
        547  bytes sent via SQL*Net to client
        377  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          2  sorts (memory)
          0  sorts (disk)
          4  rows processed

SQL> select e.last_name,e.department_id, d.department_name
  2  from employees e, departments d
  3  where e.department_id = d.department_id(+)
  4  /

LAST_NAME            DEPARTMENT_ID DEPARTMENT_NAME
-------------------- ------------- --------------------
eygle                            1 alibaba
bity_rainy                       2 oracle
piner                            3 ibm
coolyl                           5


执行计划
----------------------------------------------------------
Plan hash value: 2296652067

--------------------------------------------------------------------------------
--

| Id  | Operation          | Name        | Rows  | Bytes | Cost (%CPU)| Time
 |

--------------------------------------------------------------------------------
--

|   0 | SELECT STATEMENT   |             |     4 |   200 |     7  (15)| 00:00:01
 |

|*  1 |  HASH JOIN OUTER   |             |     4 |   200 |     7  (15)| 00:00:01
 |

|   2 |   TABLE ACCESS FULL| EMPLOYEES   |     4 |   100 |     3   (0)| 00:00:01
 |

|   3 |   TABLE ACCESS FULL| DEPARTMENTS |     4 |   100 |     3   (0)| 00:00:01
 |

--------------------------------------------------------------------------------
--


Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID"(+))

Note
-----
   - dynamic sampling used for this statement


统计信息
----------------------------------------------------------
          7  recursive calls
          0  db block gets
         31  consistent gets
          0  physical reads
          0  redo size
        547  bytes sent via SQL*Net to client
        377  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          2  sorts (memory)
          0  sorts (disk)
          4  rows processed

SQL>

 


----3. 右外连接 right outer join
select e.last_name,e.department_id,d.department_name
from employees e
right outer join departments d
on (e.department_id = d.department_id);

select e.last_name, d.department_id,d.department_name
from employees e, departments d
where e.department_id(+) = d.department_id;
结果为:所有员工及对应部门的记录,包括没有任何员工的部门记录。

 

SQL> select e.last_name, d.department_id,d.department_name
  2  from employees e, departments d
  3  where e.department_id(+) = d.department_id
  4  /

LAST_NAME            DEPARTMENT_ID DEPARTMENT_NAME
-------------------- ------------- --------------------
eygle                            1 alibaba
bity_rainy                       2 oracle
piner                            3 ibm
                                 4 sun


执行计划
----------------------------------------------------------
Plan hash value: 2392326604

--------------------------------------------------------------------------------
--

| Id  | Operation          | Name        | Rows  | Bytes | Cost (%CPU)| Time
 |

--------------------------------------------------------------------------------
--

|   0 | SELECT STATEMENT   |             |     4 |   200 |     7  (15)| 00:00:01
 |

|*  1 |  HASH JOIN OUTER   |             |     4 |   200 |     7  (15)| 00:00:01
 |

|   2 |   TABLE ACCESS FULL| DEPARTMENTS |     4 |   100 |     3   (0)| 00:00:01
 |

|   3 |   TABLE ACCESS FULL| EMPLOYEES   |     4 |   100 |     3   (0)| 00:00:01
 |

--------------------------------------------------------------------------------
--


Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("E"."DEPARTMENT_ID"(+)="D"."DEPARTMENT_ID")

Note
-----
   - dynamic sampling used for this statement


统计信息
----------------------------------------------------------
          7  recursive calls
          0  db block gets
         31  consistent gets
          0  physical reads
          0  redo size
        544  bytes sent via SQL*Net to client
        377  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          2  sorts (memory)
          0  sorts (disk)
          4  rows processed

SQL> select e.last_name,e.department_id,d.department_name
  2  from employees e
  3  right outer join departments d
  4  on (e.department_id = d.department_id);

LAST_NAME            DEPARTMENT_ID DEPARTMENT_NAME
-------------------- ------------- --------------------
eygle                            1 alibaba
bity_rainy                       2 oracle
piner                            3 ibm
                                   sun


执行计划
----------------------------------------------------------
Plan hash value: 2392326604

--------------------------------------------------------------------------------
--

| Id  | Operation          | Name        | Rows  | Bytes | Cost (%CPU)| Time
 |

--------------------------------------------------------------------------------
--

|   0 | SELECT STATEMENT   |             |     4 |   200 |     7  (15)| 00:00:01
 |

|*  1 |  HASH JOIN OUTER   |             |     4 |   200 |     7  (15)| 00:00:01
 |

|   2 |   TABLE ACCESS FULL| DEPARTMENTS |     4 |   100 |     3   (0)| 00:00:01
 |

|   3 |   TABLE ACCESS FULL| EMPLOYEES   |     4 |   100 |     3   (0)| 00:00:01
 |

--------------------------------------------------------------------------------
--


Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("E"."DEPARTMENT_ID"(+)="D"."DEPARTMENT_ID")

Note
-----
   - dynamic sampling used for this statement


统计信息
----------------------------------------------------------
          7  recursive calls
          0  db block gets
         31  consistent gets
          0  physical reads
          0  redo size
        542  bytes sent via SQL*Net to client
        377  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          2  sorts (memory)
          0  sorts (disk)
          4  rows processed

SQL>
SQL>

 

----4.FULL OUTER JOIN:全外关联
select e.last_name, e.department_id, d.department_name
from employees e
full outer join departments d
on (e.department_id = d.department_id);

 

SQL> l
  1  select e.last_name, e.department_id, d.department_name
  2  from employees e
  3  full outer join departments d
  4* on (e.department_id = d.department_id)
SQL> /

LAST_NAME            DEPARTMENT_ID DEPARTMENT_NAME
-------------------- ------------- --------------------
eygle                            1 alibaba
bity_rainy                       2 oracle
piner                            3 ibm
coolyl                           5
                                   sun


执行计划
----------------------------------------------------------
Plan hash value: 804703286

------------------------------------------------------------------------------------
| Id  | Operation            | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |             |     5 |   185 |    13   (8)| 00:00:01 |
|   1 |  VIEW                |             |     5 |   185 |    13   (8)| 00:00:01 |
|   2 |   UNION-ALL          |             |       |       |            |          |
|*  3 |    HASH JOIN OUTER   |             |     4 |   608 |     7  (15)| 00:00:01 |
|   4 |     TABLE ACCESS FULL| EMPLOYEES   |     4 |   404 |     3   (0)| 00:00:01 |
|   5 |     TABLE ACCESS FULL| DEPARTMENTS |     4 |   204 |     3   (0)| 00:00:01 |
|*  6 |    HASH JOIN ANTI    |             |     1 |    38 |     7  (15)| 00:00:01 |
|   7 |     TABLE ACCESS FULL| DEPARTMENTS |     4 |   100 |     3   (0)| 00:00:01 |
|   8 |     TABLE ACCESS FULL| EMPLOYEES   |     4 |    52 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID"(+))
   6 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")

Note
-----
   - dynamic sampling used for this statement


统计信息
----------------------------------------------------------
          0  recursive calls
          0  db block gets
         29  consistent gets
          0  physical reads
          0  redo size
        558  bytes sent via SQL*Net to client
        377  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          5  rows processed

SQL>

 

SQL>
SQL> select nvl(e.last_name,'NO value!!'), nvl(e.department_id,999), nvl(d.department_name,'NO value Too!!')
  2  from employees e
  3  full outer join departments d
  4  on (e.department_id = d.department_id);

NVL(E.LAST_NAME,'NOV NVL(E.DEPARTMENT_ID,999) NVL(D.DEPARTMENT_NAM
-------------------- ------------------------ --------------------
eygle                                       1 alibaba
bity_rainy                                  2 oracle
piner                                       3 ibm
coolyl                                      5 NO value Too!!
NO value!!
                               999 sun


执行计划
----------------------------------------------------------
Plan hash value: 804703286

------------------------------------------------------------------------------------
| Id  | Operation            | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |             |     5 |   185 |    13   (8)| 00:00:01 |
|   1 |  VIEW                |             |     5 |   185 |    13   (8)| 00:00:01 |
|   2 |   UNION-ALL          |             |       |       |            |          |
|*  3 |    HASH JOIN OUTER   |             |     4 |   608 |     7  (15)| 00:00:01 |
|   4 |     TABLE ACCESS FULL| EMPLOYEES   |     4 |   404 |     3   (0)| 00:00:01 |
|   5 |     TABLE ACCESS FULL| DEPARTMENTS |     4 |   204 |     3   (0)| 00:00:01 |
|*  6 |    HASH JOIN ANTI    |             |     1 |    38 |     7  (15)| 00:00:01 |
|   7 |     TABLE ACCESS FULL| DEPARTMENTS |     4 |   100 |     3   (0)| 00:00:01 |
|   8 |     TABLE ACCESS FULL| EMPLOYEES   |     4 |    52 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID"(+))
   6 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")

Note
-----
   - dynamic sampling used for this statement


统计信息
----------------------------------------------------------
         13  recursive calls
          0  db block gets
         61  consistent gets
          0  physical reads
          0  redo size
        637  bytes sent via SQL*Net to client
        377  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          4  sorts (memory)
          0  sorts (disk)
          5  rows processed

SQL>

 

 

 


结果为:所有员工及对应部门的记录,包括没有对应部门编号department_id的员工记录和没有任何员工的部门记录。

 

from:http://hi.baidu.com/changkongyinxue/blog/item/e32c38faffc23c9058ee90a3.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值