📌  相关文章
📜  门| Sudo GATE 2020 Mock I(2019年12月27日)|第61章

📅  最后修改于: 2021-06-29 01:38:37             🧑  作者: Mango

考虑以下数据库架构:

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 ) )

(B)

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 )

(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)
说明:请注意,只有在没有平局的情况下,以下内容才有效:即,只有一位作者的书籍数量最多:

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)是正确的。
这个问题的测验