Answer:
use this as before to get the final result:
SELECT Branch_id, Branch_name, Book_id, Title, No_of_copies-NumCheckedOut AS OnShelf
FROM LIBRARY_BRANCH NATURAL JOIN BOOK_COPIES NATURAL JOIN
( ( SELECT Branch_id, Book_id, COUNT(*) AS NumCheckedOut
FROM BOOK_LOANS
WHERE Date_out < '2006-10-14' AND Due_date > '2006-10-14'
GROUP BY Branch_id, Book_id )
UNION
( SELECT Branch_id, Book_id, 0 AS NumCheckedOut
FROM BOOK_COPIES BC
WHERE NOT EXISTS ( SELECT *
FROM BOOK_LOANS BL
WHERE BC.Branch_id=BL.Branch_id AND BC.Book_id=BL.Book_id AND
Date_out < '2006-10-14' AND Due_date > '2006-10-14') ) ) A NATURAL JOIN BOOK
Delete any entries in BOOK_COPIES where the number of copies is 0.
DELETE
FROM BOOK_COPIES
WHERE No_of_copies = 0
Add 2 copies to each book owned by Wood Library.
The following works as long as there is only one library branch named 'Wood Library'. Use IN or = ANY to account for the possibility of multiple branches with the same name.
UPDATE BOOK_COPIES
SET No_of_copies = No_of_copies+2
WHERE Branch_id = ( SELECT Branch_id
FROM LIBRARY_BRANCH
WHERE Branch_name='Wood Library' )
Change the due date of any book checked out from Wood Library that is due before 2006-11-05 to 2006-12-31.
The following works as long as there is only one library branch named 'Wood Library'. Use IN or = ANY to account for the possibility of multiple branches with the same name.
UPDATE BOOK_LOANS
SET Due_date='2006-12-31'
WHERE Due_date < '2006-11-05' AND Branch_id = ( SELECT Branch_id
FROM LIBRARY_BRANCH
WHERE Branch_name='Wood Library' )