R(X,Y)÷S(Y) 等价于
Select distinct R.X from R R1
Where not exists(
Select * from S
Where not exists(
Select * from R R2
Where R2.X = R1.X and R2.Y = S.Y
)
)
以万年例子关系模式SC(sno,cno)与Course为例。
SC÷Course得到就是选了全部课程的学生学号,即95001.
现在开始分析SQL语句除法过程:
SC÷Course得到选了全部课程的学生学号=得到学号,该学生没有一门课没选。
这里使用了双重否定。
第一层:该学生一门都没选。假定该学生学号为95001
select * from course c
where not exists(
select * from sc
where sc.sno=95001 and sc.cno = c.cno
)
这次查寻过程如下:选取Course的第1个元组(1),与该学生95001结合,发现sc中存在(95001,1),即95001选了1号课,由于not exists ,故course第一个元组(1)排除了,不显示;接下来按照上诉,一次选取course元组进行操作,发现结果什么都没显示,空集。
第二层
not exists(
select * from course c
where not exists(
select * from sc
where sc.sno=95001 and sc.cno = c.cno
)
)
按照上诉分析,对于95001这个学号,not exists(空集)=true。即95001选了全部课程,接下来循环遍历检查学号就可以得到全部要求的学号了。
select sc1.sno from SC sc1
where not exists(
select * from Course c
where not exists(
select * from SC sc2
where sc2.sno.sc1.sno and sc2.cno = c.cno
)
)
多多过几遍过程就会了。
[例]查询至少选修了学生95002选修的全部课程的学生号码。
select distinct sc1.sno from SC sc1
where not exists(
select * from SC sc2
where sc2.sno = 95002 and
not exists(
select * from SC sc3
where sc3.sno = sc1.sno and sc3.cno = sc2.cno
)
)