Oracle连接全接触

CREATE TABLE SCOTT.EMPLOYEES
(
   employee_id varchar2(5) not null,
   last_name varchar2(20),
   department_id varchar2(5)
)

INSERT INTO SCOTT.EMPLOYEES(EMPLOYEE_ID,LAST_NAME,DEPARTMENT_ID)
       VALUES('100','king','90');
INSERT INTO SCOTT.EMPLOYEES(EMPLOYEE_ID,LAST_NAME,DEPARTMENT_ID)
       VALUES('101','kochhar','90');
INSERT INTO SCOTT.EMPLOYEES(EMPLOYEE_ID,LAST_NAME,DEPARTMENT_ID)
       VALUES('102','Tom','90');
INSERT INTO SCOTT.EMPLOYEES(EMPLOYEE_ID,LAST_NAME,DEPARTMENT_ID)
       VALUES('103','kochhar','90');
INSERT INTO SCOTT.EMPLOYEES(EMPLOYEE_ID,LAST_NAME,DEPARTMENT_ID)
       VALUES('202','Fay','20');
INSERT INTO SCOTT.EMPLOYEES(EMPLOYEE_ID,LAST_NAME,DEPARTMENT_ID)
       VALUES('203','Jim','20');
INSERT INTO SCOTT.EMPLOYEES(EMPLOYEE_ID,LAST_NAME,DEPARTMENT_ID)
       VALUES('204','Green','50');
INSERT INTO SCOTT.EMPLOYEES(EMPLOYEE_ID,LAST_NAME,DEPARTMENT_ID)
       VALUES('205','Higgins','110');
INSERT INTO SCOTT.EMPLOYEES(EMPLOYEE_ID,LAST_NAME,DEPARTMENT_ID)
       VALUES('206','Gietz','110');
      
CREATE TABLE SCOTT.DEPARTMENTS
(
       department_id varchar2(5) not null,
       department_name varchar2(20),
       location_id varchar2(50)
)

insert into scott.departments(department_id,department_name,location_id) values('20','Marketing','1800');
insert into scott.departments(department_id,department_name,location_id) values('40','Shopping','2000');
insert into scott.departments(department_id,department_name,location_id) values('90','IT','1400');
insert into scott.departments(department_id,department_name,location_id) values('110','Sales','2500');


--- 笛卡尔乘积 等于 交叉连接
--笛卡尔乘积 employees * department(Oracle 8i,Oracle 9i)
select  employees.EMPLOYEE_ID,employees.LAST_NAME,employees.DEPARTMENT_ID,
        departments.department_id,departments.department_name,departments.location_id
from scott.employees  ,scott.departments   --交换位置连接结果不表

--交叉连接  employees * department(Oracle 9i)
select  employees.EMPLOYEE_ID,employees.LAST_NAME,employees.DEPARTMENT_ID,
        departments.department_id,departments.department_name,departments.location_id
from scott.employees 
 cross join scott.departments   --交换位置连接结果不表


--对两张表中都有的数据做连接
select  employees.EMPLOYEE_ID,employees.LAST_NAME,employees.DEPARTMENT_ID,
        departments.department_id,departments.department_name,departments.location_id
from scott.employees  ,scott.departments   --交换位置连接结果不表
where   employees.department_id = departments.department_id --交换位置结果不变
order by employees.employee_id

--自然连接
--基于两个表之间有相同名字的所有列
--它从两个表中选择在所有的匹配列中有相等值的行
--注意:不能使用限定词
select  *
from  scott.employees
 natural join   scott.departments --交换位置连接结果不表
order by employee_id

--对两张表中都有的数据做连接
select  employees.EMPLOYEE_ID,employees.LAST_NAME,employees.DEPARTMENT_ID,
        departments.department_id,departments.department_name,departments.location_id
from  scott.employees
  JOIN    scott.departments  --交换位置连接结果不表
  ON   employees.department_id = departments.department_id --交换位置结果不变
order by employees.employee_id

 

--对两张表中都有的数据做连接 Employees左连接
--用Employees表的所有数据连接Department表(可能为空)
select  employees.EMPLOYEE_ID,employees.LAST_NAME,employees.DEPARTMENT_ID,
        departments.department_id,departments.department_name,departments.location_id
from scott.employees  ,scott.departments   --交换位置连接结果不表
where   employees.department_id = departments.department_id(+) --交换位置结果不变
order by employees.employee_id


select  employees.EMPLOYEE_ID,employees.LAST_NAME,employees.DEPARTMENT_ID,
        departments.department_id,departments.department_name,departments.location_id
from   scott.employees
    LEFT OUTER JOIN   scott.departments --交换位置连接结果变化
    ON   employees.department_id = departments.department_id --交换位置结果不变
order by employees.employee_id


--对两张表中都有的数据做连接 Employees右连接
--用Department表的所有数据连接Employees表(可能为空)
select  employees.EMPLOYEE_ID,employees.LAST_NAME,employees.DEPARTMENT_ID,
        departments.department_id,departments.department_name,departments.location_id
from scott.employees, scott.departments    --交换位置连接结果不表
where   employees.department_id(+) = departments.department_id --交换位置结果不变
order by employees.employee_id

select  employees.EMPLOYEE_ID,employees.LAST_NAME,employees.DEPARTMENT_ID,
        departments.department_id,departments.department_name,departments.location_id
from   scott.employees
    RIGHT OUTER JOIN   scott.departments --交换位置连接结果变化
    ON   employees.department_id = departments.department_id --交换位置结果不变
order by employees.employee_id

--从中可以看出Employees左连接等于Department的右连接


--全连接
select  employees.EMPLOYEE_ID,employees.LAST_NAME,employees.DEPARTMENT_ID,
        departments.department_id,departments.department_name,departments.location_id
from   scott.employees
    FULL OUTER JOIN    scott.departments--交换位置连接结果变化
    ON   employees.department_id = departments.department_id --交换位置结果不变
order by employees.employee_id

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值