数据库系统入门 | Not Exisits 结构的灵活应用

教材 /《数据库系统概念》第六版第三章内容 机械工程出版社;实验软件/Qracle 11g

写在前面

用下面的样例1引出我们讨论的这一类方法。

样例1:使用大学模式,用SQL写出以下查询,实现功能:找出选修了Biology系开设的所有课程的学生。

分析:
首先,我们思考这样一个问题。假设我们将本题中各关系用以下方法定义
A:找出学生所修的所有课程
B:找出所有生物系所开设的课程

思路:
本样例想要的查找结果必然满足:

而相对应的,相差 可以由 minus 实现,为空 可以由 not exists 实现。由此,我们可以写出此查询如下:

select s.ID , s.name
from student s
where not exists
(
(select course_id
from course
where dept_name='Biology')
minus
(select t.course_id
from takes t
where s.ID=t.ID)
);

下面提供一个类似的样例2查询题。

样例2: 考虑如下的图书馆数据库。

member(memb no, name, age)
book(isbn, title, authors, publisher)
borrowed(memb no, isbn, date)

题目要求 用SQL写出如下查询,实现功能:打印借阅了所有由McGraw-Hill出版的书的会员的名字。

select name
  from member m
 where not exists
          ( (select isbn
               from book
              where publisher = 'McGraw_Hill')
           minus
           (select isbn
              from borrowed b
             where b.memb_no = m.memb_no));
原文地址:https://www.cnblogs.com/zhouie/p/10702611.html