Database Systems: Homework 1 Key Due 18 September, 2013 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 P Q R 10 a 5 15 b 8 25 a 6 Table T 2 A B C 10 b 6 25 c 3 10 b 5 (a) T 1 1T 1.P =T 2.A P Q R A B 10 a 5 10 b T2 10 a 5 10 b 25 a 6 25 c P (b) T 1 1T 1.Q=T 2.B T 2 15 15 Q R A b 8 10 b 8 10 B C b 6 b 5 P Q R A B 10 a 5 10 b (c) T 1 ./T 1.P =T 2.A T 2 10 a 5 10 b 15 b 8 ω ω 25 a 6 25 c P 15 (d) T 1 ./ T 1.Q=T 2.B T 2 ω 15 Q R A b 8 10 ω ω 25 b 8 10 C 6 5 3 C 6 5 ω 3 B C b 6 c 3 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 (f) T 1 1T 1.P =T 2.A AND T 1.R=T 2.C T 2 P Q R A B 10 a 5 10 b 1 C 5 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 appropriate domains, constraints and referential triggered actions. CREATE TABLE Book ( Int PRIMARY KEY, Book id Title Varchar(200), Publisher name Varchar(200), FOREIGN KEY (Publisher name) REFERENCES Publisher(Name) ON DELETE SET NULL ON UPDATE CASCADE ); CREATE TABLE Book Authors ( Int NOT NULL, Book id Varchar(200) NOT NULL, Author name PRIMARY KEY (Book id, Author name), FOREIGN KEY (Book id) REFERENCES Book(Book id) ON DELETE CASCADE ON UPDATE CASCADE ); CREATE TABLE Name Address Phone ); Publisher ( Varchar(200) Varchar(400), Decimal(20) PRIMARY KEY, CREATE TABLE Book Copies ( Int NOT NULL, Book id Branch id Char(4) NOT NULL, No of copies Int DEFAULT 1, PRIMARY KEY (Book id, Branch id), FOREIGN KEY (Book id) REFERENCES Book(Book id) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (Branch id) REFERENCES Library Branch(Branch id) ON DELETE CASCADE ON UPDATE CASCADE ); CREATE TABLE Book Loans ( Book id Int NOT Branch id Char(4) NOT Card no Int NOT Date out Date, Due date Date, PRIMARY KEY (Book id, Branch 2 NULL, NULL, NULL, id, Card no), 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 ); CREATE TABLE Library Branch ( Char(4) Branch id Branch name Varchar(200) Address Varchar(400) ); PRIMARY KEY, NOT NULL, CREATE TABLE Borrower ( Card no Int PRIMARY KEY, Name Varchar(200) NOT NULL, Address Varchar(400), Phone Decimal(20) ); (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. INSERT INTO Borrower VALUES (328820001, 0 Martin Fisher0 , 0 123 Fake St, Springfield0 , 4065822400); (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. UPDATE Book Copies SET No of copies = No of copies+1 WHERE Book id IN ( SELECT Book id FROM Book WHERE Title=0 Here Comes a Candle0 ) AND Branch id IN ( SELECT Branch id FROM Library Branch WHERE Branch name=0 Bozeman0 ); 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. SELECT Fname, Minit, Lname FROM Employee WHERE Super ssn IN ( SELECT Ssn FROM Employee WHERE Super ssn = 888665555 ); (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. SELECT Fname, Minit, Lname FROM Employee WHERE Salary >= 10000+(SELECT min(Salary) FROM Employee); 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’ ? Sharps id ← πBranch id (σBranch name=0 Sharpstown0 (Library Branch)) Tribe id ← πBook id (σTitle=0 The Lost Tribe0 (Book)) Answer ← πNo of copies (Book Copies ∗ Sharps id ∗ Tribe id) -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. None id ← πCard no (Borrower) − πCard no (Book Loans) Answer ← πName (Borrower ∗ None id) (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. (6 pts) In relational models, primary keys based on existing, meaningful attributes of the tuples are known as natural keys. Some database designers prefer to add an extra attribute that doesn’t model anything about the miniworld; it is specifically generated solely to be a primary key. These are called surrogate keys. Discuss the advantages and disadvantages of both approaches. 5 Figure 4.6 6 Figure 3.5 7