考虑以下数据库架构:
BOOK(Book_id, Title, Publisher_name)
BOOK_AUTHORS(Book_id, Author_name)
PUBLISHER(Name, Address, Phone)
BOOK_COPIES(Book_id, Branch_id, No_of_copies)
BOOK_LOANS(Book_id, Branch_id, Card_no, Date_out, Due_date)
LIBRARY_BRANCH(Branch_id, Branch_name, Address)
BORROWER(Card_no, Name, Address, Phone)
请注意,BOOK_COPIES 仅包含 No_of_copies 至少为 1 的条目——如果分支不包含某本书的任何副本,则 BOOK_COPIES 中没有条目(不是具有 0 个副本的条目)。
关键属性带有下划线。外键约束如下:
- BOOK 中的 Publisher_name 指的是 PUBLISHER 中的 Name。
- BOOK_AUTHORS、BOOK_COPIES 和 BOOK_LOANS 中的 Book_id 属性均指 BOOK 中的 Book_id。
- BOOK_COPIES 和 BOOK_LOANS 中的 Branch_id 属性均指 LIBRARY_BRANCH 中的 Branch_id。
- BOOK_LOANS 中的 Card_no 是指 BORROWER 中的 Card_no。
我们想找到出书数量最多的作者所写的所有书的书名。以下哪个查询不正确?
(假设可以有领带。)。
(一种)
SELECT DISTINCT Title
FROM BOOK NATURAL JOIN BOOK_AUTHORS
WHERE Author_name IN ( SELECT Author_name
FROM BOOK NATURAL JOIN BOOK_AUTHORS
GROUP BY Author_name
HAVING COUNT(*) >= ALL ( SELECT COUNT(*)
FROM BOOK NATURAL JOIN BOOK_AUTHORS
GROUP BY Author_name ) )
(乙)
SELECT DISTINCT Title
FROM BOOK NATURAL JOIN BOOK_AUTHORS
WHERE Author_name = ANY ( SELECT Author_name
FROM BOOK NATURAL JOIN BOOK_AUTHORS
GROUP BY Author_name
HAVING COUNT(*) >= ALL ( SELECT COUNT(*)
FROM BOOK NATURAL JOIN BOOK_AUTHORS
GROUP BY Author_name ) )
(C)
SELECT DISTINCT Title
FROM BOOK NATURAL JOIN BOOK_AUTHORS A
WHERE ( SELECT COUNT(*)
FROM BOOK_AUTHORS A2
WHERE A.Author_name=A2.Author_name ) >= ALL ( SELECT COUNT(*)
FROM BOOK NATURAL JOIN BOOK_AUTHORS
GROUP BY Author_name )
(四)
SELECT DISTINCT Title
FROM BOOK NATURAL JOIN BOOK_AUTHORS
WHERE Author_name = ( SELECT Author_name
FROM BOOK NATURAL JOIN BOOK_AUTHORS
GROUP BY Author_name
HAVING COUNT(*) >= ALL ( SELECT COUNT(*)
FROM BOOK NATURAL JOIN BOOK_AUTHORS
GROUP BY Author_name ) )
答案: (D)
说明:请注意,以下仅适用于不存在平局的情况,即只有一位作者拥有最多的书籍:
SELECT DISTINCT Title
FROM BOOK NATURAL JOIN BOOK_AUTHORS
WHERE Author_name = ( SELECT Author_name
FROM BOOK NATURAL JOIN BOOK_AUTHORS
GROUP BY Author_name
HAVING COUNT(*) >= ALL ( SELECT COUNT(*)
FROM BOOK NATURAL JOIN BOOK_AUTHORS
GROUP BY Author_name ) )
选项(D)是正确的。
这个问题的测验