Uploaded by shwkt Mohamed Moahmed Elshahd

Q3

advertisement
Riyadh Philanthropic Society For Science
Prince Sultan College For Woman
Dept. of Computer & Information Sciences
CS 340
Introduction to Database Systems
(Chapter 6 Practice Exercise)
Exercise 1
Consider the LIBRARY relational database schema below, which is
used to keep track of books, borrowers, and book loans. Referential
integrity constraints are shown as directed arcs. Write down relational
expressions for the following queries:
Chapter 6 Practice Exercise
1
Exercise 1 - a
How many copies of the book titled ‘The Lost Tribe’ are owned by
the library branch whose name is ‘Sharpstown’?
TEMP
BOOK * BOOK_COPIES * LIBRARY_BRANCH
RESULT p No-Of-Copies ( BranchName=‘Sharpstown’ AND Title=‘The Lost Tribe’ (TEMP))
Chapter 6 Practice Exercise
2
Exercise 1 - b
How many copies of the book titled ‘The Lost Tribe’ are owned by
each library branch?
THE_LOST_TRIBE
RESULT
 Title=‘The Lost Tribe’ (BOOK)
p BranchId, No-Of_Copies (THE_LOST_TRIBE * BOOK_COPIES)
Chapter 6 Practice Exercise
3
Exercise 1 - c
Retrieve the names of all borrowers who do not have any books
checked out.
NO_CHECHOUT
RESULT
p CardNo (BORROWER) - p CardNo (BOOK_LOANS)
p Name (NO_CHECKOUT * BORROWER)
Chapter 6 Practice Exercise
4
Exercise 1 - d
For each book that is loaned out from the ‘Sharpstown’ branch and
whose DueDate is today, retrieve the book title, the borrower’s name,
and the borrower’s address.
ST
p BranchId ( BranchName=‘Sharpstown’ (LIBRARY_BRANCH))
DUE_TODAY
 DueDate=‘Today’ (BOOK_LOANS)
ST_BOOKS
p BookId, CardNo (ST * DUE_TODAY)
RESULT
p Title, Name, Address (BOOK * ST_BOOKS * BORROWER)
Chapter 6 Practice Exercise
5
Exercise 1 - e
For each library branch, retrieve the branch name and the total
number of books loaned out from that branch.
LOANED_B
RESULT
BranchId
Count BookId (BOOK_LOANS)
p BranchName, Count_BookId (LOANED_B * LIBRARY_BRANCH)
Chapter 6 Practice Exercise
6
Exercise 1 - f
Retrieve the names, addresses, and number of books checked out for
all borrowers who have more than five books checked out.
BORROWED_B
CardNo Count BookId (BOOK_LOANS)
BORROWED_5
 Count_BookId>5 (BORROWED_B)
RESULT
p Name, Address, Count_BookId (BORROWED_5 * BORROWER)
Chapter 6 Practice Exercise
7
Exercise 1 - g
For each book authored (or coauthored) by ‘Stephen King,’ retrieve
the title and the number of copies owned by the library branch whose
name is ‘Central.’
SK
 AuthorName=‘Stephen King’ (BOOK_AUTHORS)
SK_B
p BookId, Title (SK * BOOK)
CENTRAL
 BranchName=‘Central’ (LIBRARY_BRANCH)
RESULT
p Title, No_Of_Copies (SK_B * BOOK_COPIES * CENRAL)
Chapter 6 Practice Exercise
8
Download