You are on page 1of 2

For the given relational schema, the primary keys and foreign keys are as follows:

Member(member_no, name, class)


Primary key: member_no
Book(isbn, title, author, publisher)
Primary key: isbn
Borrowed(member_no, isbn, date)
Primary key: (member_no, isbn)
Foreign key: member_no referencing Member(member_no)
Foreign key: isbn referencing Book(isbn)
Therefore, the primary keys in this schema are:

Member: member_no
Book: isbn
Borrowed: (member_no, isbn)
And the foreign keys are:

Borrowed.member_no references Member.member_no


Borrowed.isbn references Book.isbn

i. Find the title, author and publisher of the books:


π_title,_author,_publisher (σ_isbn=_isbn (Book ⨝ Author))

ii. Find the member number and name of each member in second year class:
π_member_no,_name (σ_class="second year" (Member))

iii. Find the member numbers which rented the book "DBMS" on '2-1-2019':
π_member_no (σ_isbn="DBMS" ∧ _date="2-1-2019" (Borrowed))

iv. Find the title of books which has borrowed by the member John:
π_title (σ_name="John" (Member ⨝ Borrowed ⨝ Book))

v. Find the member numbers which dont rent the "DBMS" book:
π_member_no (σ_isbn≠"DBMS" (Borrowed))

vi. Find the number of members who borrow more than five books:
π_count (γ_count (σ_count > 5 (γ_count (π_member_no,_count (Borrowed ⨝ π_count
(Book ⨝ π_member_no (Member)) ⨝ π_member_no (Member)))))

i. Find the member number and name of each member who has borrowed at least one
book published by "MCGraw-Hill":
SELECT DISTINCT member_no, name
FROM Member
WHERE member_no IN (
SELECT member_no
FROM Borrowed
JOIN Book ON Borrowed.isbn = Book.isbn
WHERE publisher = 'MCGraw-Hill'
);

ii. Create a new member name is 'Mozart' with member number '12121':
INSERT INTO Member (member_no, name, class)
VALUES ('12121', 'Mozart', 'N/A');

iii. Find the member whose name begin with the letter 'K':
SELECT * FROM Member
WHERE name LIKE 'K%';

iv. Find the total numbers of books which borrowed for each member:
SELECT member_no, COUNT(*)
FROM Borrowed
GROUP BY member_no;

v. Find the title of books borrowed by the member Mozart:


SELECT b.title
FROM Borrowed b
JOIN Member m ON b.member_no = m.member_no
WHERE m.name = 'Mozart';

vi. For each publisher, find the member number and name of each member who has
borrowed more than five books of that publisher:
SELECT publisher, m.member_no, m.name
FROM Borrowed
JOIN Book ON Borrowed.isbn = Book.isbn
JOIN Member m ON Borrowed.member_no = m.member_no
GROUP BY publisher, m.member_no, m.name
HAVING COUNT(*) > 5;

vii. Find the number of members who borrow more than the number of books borrowed
by Mozart:
SELECT COUNT(*)
FROM Member m
WHERE (SELECT COUNT(*) FROM Borrowed WHERE Borrowed.member_no = m.member_no) >
(SELECT COUNT(*) FROM Borrowed b JOIN Book ON b.isbn = Book.isbn WHERE
Book.publisher = 'MCGraw-Hill' AND b.member_no = m.member_no);

viii. Add 'phone number' column into member table:


ALTER TABLE Member
ADD COLUMN phone_number VARCHAR(20);

ix. Change the name of member table to the name of student:


ALTER TABLE Member
RENAME TO student;

x. Delete all members in first_year class:


DELETE FROM Member
WHERE class = 'first_year';

You might also like