开发工具与关键技术:PLSQL Developer、Oracle、SQL*plus
一.SET操作符的主要作用就是返回两个查询结果集的并集、交集、差集。
二.在使用SET操作符时注意:在select列表中的列名和表达式在数量和数据类型上要相对应,否则运行结果如下:
三.(1)union —— 返回两个查询的结果集的并集。
例:查询所有教师和同学的name(姓名)、sex(性别)和birthday(生日).
首先查询所有同学:select sname,ssex,sbirthday from student
接着查询所有老师:select tname,tsex,tbirthday from teacher
然后通过union 求以上两个查询的结果集的并集:
select sname,ssex,sbirthday from student
union
select tname,tsex,tbirthday from teacher
并集执行结果:
(2)intersect —— 返回两个查询的结果集的交集。
例:查询“计算机系”与“电子工程系”不同职称的教师的Tname(姓名)和Prof(职称)。
select tname,prof
from teacher
where prof not in (select prof from teacher where depary in ‘计算机系’
intersect
select prof from teacher where depary in ‘电子工程系’ )
计算机系教师的职称:
电子工程系教师的职称:
通过intersect取交集:
整段执行结果:
(3)minus —— 返回两个查询的结果集的差集。
例:查询所有未讲课的教师的Tname和Depart
首先查询出所有教师:select tname,depary from teacher
再通过join on联表查询出所有讲过课的老师:
select distinct t.tname, t.depary
from teacher t
join course s
on s.tno=t.tno
join score r
on r.cno=s.cno
然后通过minus求以上两个查询的结果集的差集:
( 注意:在minus上方查询的结果集要大于minus下方查询的结果集)
select tname,depary from teacher
minus
select distinct t.tname, t.depary
from teacher t
join course s
on s.tno=t.tno
join score r
on r.cno=s.cno
差集执行结果: