Database Systems: Homework 1 Key: T1.P T2.A
Database Systems: Homework 1 Key: T1.P T2.A
Team:
1. (2 pts each) Consider the two tables T 1 and T 2. Show the results of the following
relational algebra operations:
Table T 1 Table T 2
P Q R A B C
10 a 5 10 b 6
15 b 8 25 c 3
25 a 6 10 b 5
P Q R A B C
10 a 5 10 b 6
(a) T 1 1T 1.P =T 2.A T2
10 a 5 10 b 5
25 a 6 25 c 3
P Q R A B C
(b) T 1 1T 1.Q=T 2.B T 2 15 b 8 10 b 6
15 b 8 10 b 5
P Q R A B C
10 a 5 10 b 6
(c) T 1 ./T 1.P =T 2.A T 2 10 a 5 10 b 5
15 b 8 ω ω ω
25 a 6 25 c 3
P Q R A B C
15 b 8 10 b 6
(d) T 1 ./ T 1.Q=T 2.B T 2
ω ω ω 25 c 3
15 b 8 10 b 5
P Q R
10 a 5
15 b 8
(e) T 1 ∪ T 2 25 a 6
10 b 6
25 c 3
10 b 5
P Q R A B C
(f) T 1 1T 1.P =T 2.A AND T 1.R=T 2.C T 2
10 a 5 10 b 5
1
2. Refer to figure 4.6, a schema diagram for a library database, for these questions:
(a) (6 pts) Write the SQL DDL statements to define this database. Include appro-
priate domains, constraints and referential triggered actions.
2
FOREIGN KEY (Book id) REFERENCES Book(Book id)
ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (Branch id) REFERENCES Library Branch(Branch id)
ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (Card no) REFERENCES Borrower(Card no)
ON DELETE RESTRICT ON UPDATE CASCADE
);
(b) (4 pts) Write the SQL DML statement to insert a new borrower, h328820001,
‘‘Marten Fisher’’, ‘‘123 Fake St, Springfield’’, 406 582 2400i, in the
database.
(c) (4 pts) The Bozeman branch has acquired a second copy of the book Here Comes
a Candle. Write the SQL to update the database to increase the number of copies
for that book by one.
3. (4 pts each) Refer to figure 3.5, the schema diagram for the COMPANY database, for
these questions:
(a) Write the SQL query to retrieve the names of all employees who work in the
department that has the employee with the highest salary among all employees.
3
SELECT Fname, Minit, Lname
FROM Employee
WHERE Dno = (
SELECT Dno
FROM Employee
WHERE Salary = (
SELECT max(Salary)
FROM Employee
)
);
(b) Write the SQL query to retrieve the names of all employees whose supervisor’s
supervisor has ‘888665555’ for Ssn.
(c) Write the SQL query to retrieve the names of employees who make at least $10,000
more than the employee who is paid the least in the company.
4. (4 pts each) Refer again to figure 4.6, this time give relational algebra expressions
for the following queries:
(a) How many copies of the book titled The Lost Tribe are owned by the library
branch whose name is ‘Sharpstown’ ?
-or-
Answer ← πNo of copies (σBranch name=0 Sharpstown0 ∧Title=0 The Lost Tribe0 (
Book Copies ∗ Library Branch ∗ Book))
4
(b) Retrieve the names of all borrowers who do not have any books checked out.
(c) Retrieve the names, addresses, and number of books checked out for all borrowers
who have more than five books checked out.
Loan counts ← ρ(Card no,Book count) (Card no =Count(Book id) (Book Loans))
Big borrowers ← σBook count>5 (Loan counts)
Answer ← πName,Address,Book count (Big borrowers ∗ Borrower)
5
Figure 4.6
6
Figure 3.5