You are on page 1of 16

Page

German University in Cairo Faculty of Media Engineering and Technology Prof. Dr. Slim Abdennadher

January 11, 2012

Databases I Spring term 2011 Final Exam

Bar Code
Instructions: Read carefully before proceeding.

1) Please tick your major


Major CSEN DMET BI

2) Duration of the exam: 3 hours (180 minutes). 3) (Non-programmable) Calculators are allowed. 4) No books or other aids are permitted for this test. 5) This exam booklet contains 15 pages, including this one. Three extra sheets of scratch paper are attached and have to be kept attached. Note that if one or more pages are missing, you will lose their points. Thus, you must check that your exam booklet is complete. 6) Write your solutions in the space provided. If you need more space, write on the back of the sheet containing the problem or on the three extra sheets and make an arrow indicating that. Scratch sheets will not be graded unless an arrow on the problem page indicates that the solution extends to the scratch sheets. 7) When you are told that time is up, stop working on the test.
Good Luck!

Don't write anything below ;-) Exercise Marks Final Marks 1 8 2 12 3 12 4 6 5 8 6 13 7 8 8 5 72

Databases I , Final Exam, January 11, 2012

Page

Exercise 1

(2+6=8 Marks)

a) When is the following relational algebra expressions equivalent. Justify your answer:
X (C (S)) = C (X (S))

Solution: If the condition C uses only attributes from the set of attributes X

b) Consider two relations about restaurants and meals they serve:


restaurant(rname, cuisine, zip)

where the rname is the primary key. Examples of records in the restaurant table
rname

Bangkok Churra's Oskar's ...

cuisine

Thai Brazilian German ...

zip

97201 97203 97225 ...

open(rname, meal)

where rname, meal is the primary key. Example of records in the open table: Bangkok lunch Bangkok dinner Churra's dinner ... ... Consider the following relational algebra query over these two tables (where r = restaurant and o = open). Give three other algebra expressions that are equivalent to this query. (The expressions you give should be equivalent for any state of the database.)
r.cuisine (o.meal= dinner r.zip=97241 (o 1o.rname=r.rname r))

rname

meal

Solution: The rst alternative:


r.cuisine (r.zip=97241 (o.meal= dinner (o) 1o.rname=r.rname r))

The second alternative:


r.cuisine (o.meal= dinner (o 1o.rname=r.rname r.zip=97241 (r)))

The third alternative:


r.cuisine ((o.meal= dinner (o) 1o.rname=r.rname r.zip=97241 (r)))

Databases I , Final Exam, January 11, 2012

Page

The following two exercises are based on the Library database, which contains six relations: a) Person stores the rst name, the middle initial and last name of Librarys' head managers, books' Authors, and Students as well as their address, sex, telephone number and the students' matriculation number (if the person is a student otherwise NULL values will be provided). IDP is a serial number.
Person(FNAME, MINIT, LNAME, IDP, ADDRESS, SEX, MAT, DATEBIRTH, PHONE)

b) Author stores the correspondence among Books and the people who wrote them.
Author(ISBN, IDP)

c) Book stores the ISBN of each book of the library, its title, and its publishing date.
Book(Title, ISBN, PDate)

d) Copy stores in which library is every single copy of each book located where IDC is a serial number for dierent copies of books and IDL is the ID for the library.
Copy(IDC, ISBN, IDL)

e) Borrower stores the matriculation number of the student having currently borrowed a book and the date when he or she borrowed it. The table also contain the reservation's expiring date and a boolean eld (Returned) indicating if the book has been given back or not. IDB is a serial number for Borrower.
Borrower(IDB, IDC, MAT, RentingDate, ExperingDate, Returned)

f) Library stores a number identifying each library, its name, its address and nally the ID of its manager.
Library(LName, IDL, IDMgr, MgrStartDate)

Databases I , Final Exam, January 11, 2012

Page

Exercise 2 Formulate relational algebra expressions that answer the following queries.

(12 Marks)

a) All titles of books borrowed by the student 'Cecilia Bartoli'.


Solution:
T IT LE (Book 1Book.ISBN =Copy.ISBN Copy 1Copy.IDC=Borrowe.IDC Borrower 1Borrower.M AT =P erson.M AT (F N AM E= Cecilia LN AM E= Baroli (P erson)))

b) The full names of all library managers and the name of the library they are managing.
Solution:
P erson.F N AM E,P erson.M IN IT,P erson.LN AM E,Library.LN AM E (P erson 1P erson.IDP =Library.IDM gr Library)

c) The matriculation number and the full name of the students having no book borrowed.
Solution:

M AT,F N AM E,M IN IT,LN AM E (P erson) M AT,F N AM E,M IN IT,LN AM E (P erson 1P erson.M AT =Borrwer.M AT Borrower)

d) The ISBN and the title of books that are currently available through the library.
Solution:
Book.ISBN,Book.T IT LE ((IDC (Copy) IDC (Borrower)) 1 Copy 1 Book)

(1)

e) The name and the surname of each manager, together with the name, surname and phone number of the students having borrowed in her/his library a book before January 1, 2011 without returning it.
Solution:

P erson 1Borrower.M AT =P erson.M AT Library.IDM gr,Borrower.M AT (Borrower.returned=f alseBorrower.Rentingd ate<1/1/2011 (Borrower 1Copy.IDC=Borrower.IDC Copy 1Copy.IDL=Lib.IDL (Lib(IDL,IDM gr,M grF name,M grLname) (Library.IDL,Library.IDM gr,P erson.F N AM E,P erson.LN AM E (P erson 1Library.IDM gr=P erson.IDP Library)))))

Databases I , Final Exam, January 11, 2012

Page

Exercise 3 Formulate SQL queries that solve the tasks listed below.

(12 Marks)

a) All the data of the female students having never borrowed a book
Solution:
SELECT * FROM Person LEFT OUTER JOIN Borrowed ON Person.MAT = Borrowed.MAT WHERE Person.SEX = 'female' AND Borrowed.MAT IS NULL

b) The full name of the students being co-author of at least one Book. Coauthor: a writer who collaborates with others in writing something. That is a person who wrote a book not alone.
Solution:
SELECT Person.FNAME, Person.MINIT, Person.LNAME FROM Person INNER JOIN Author A1 ON Person.IDP = A1.IDP WHERE Person.MDP IS NOT NULL AND EXISTS (SELECT * FROM Author A2 WHERE A1.ISBN = A2.ISBN AND A1.IDP <> A2.IDP)

c) The name and the ISBN of the books for which at least 2 copies are available for rent
Solution:
SELECT B.ISBN, B.Title From Book B WHERE 2 <= ((SELECT COUNT(*) FROM Copy C WHERE B.ISBN = C.ISBN) (SELECT COUNT(*) FROM Copy C, Borrower Bo WHERE C.ISBN = Bo.ISBN AND B.ISBN = C.ISBN AND returned = false))

d) The full name of the authors having their full name longer than 15 characters and having a surname that includes the string 'ov'.
Solution:
SELECT Person.FNAME, Person.LNAME FROM Person P INNER JOIN Author A ON P.IDP = A.IDP WHERE P.LNAME LIKE '% av % ' AND LENGTH(FNAME) + LENGTH(MINIT) + LENGTH(LNAME) > 15

e) The average number of books (copies) rented now and in the past by the male students being older than the average male student. (Maybe you can use a view.). Be sure to count also as 0 the students having never borrowed a book.
Solution:

Databases I , Final Exam, January 11, 2012

Page

CREATE VIEW PV(P,C) AS SELECT P.MAT, 0 FROM Person P LEFT OUTER JOIN Borrower B ON P.MAT = B.MAT WHERE P.SEX = male AND B.MAT IS NULL UNION SELECT P.MAT, SUM(B.IDC) FROM Person P INNER JOIN Borrower B ON P.MAT = B.MAT WHERE P.SEX = male GROUP BY P.MAT GO SELECT AVG(C) FROM Person P INNER JOIN PV On PV.P = P.MAT WHERE AVG(CURRENT_DATE - P.DATEBIRTH) > (SELECT AVG(CURRENT_DATE - P2.DATEBIRTH) FROM Person P2 WHERE P2.SEX = 'Male' AND P2.MAT IS NOT NULL)

Databases I , Final Exam, January 11, 2012

Page

The following two exercises are based on the following Car database. The car dealer Omar would like to organize his inventory in a relational database system. The requirements analysis led to store information about each car that Omar converted into a relational schema. Immediately, he begins with the collection of his old data and obtained the following relation Car: modNr manNr manuf type power vehicleNr yearOfC mileage newP saleP purchaseP 1 1 Opel Kadett 60 K674 1990 10000 18000 13000 12000 1 1 Opel Kadett 60 K634 1988 34000 18000 12000 9000 2 1 Opel Vectra 90 V459 1990 15000 25000 18000 17000 3 1 Opel Omega 110 O634 1987 45000 30000 22000 15000 4 2 VW Golf 90 G789 1991 11000 25000 21000 16000 4 2 VW Golf 90 G713 1991 31000 25000 16000 13000 5 2 VW Golf 105 G762 1992 28000 28000 19000 17000 6 2 VW Beatles 60 K634 1986 71000 19000 10000 8000 The dierent car models are serially numbered (modNr). A model is dened with the manufacturer (manuf), the manufacturer number (manNr), type and engine power (power). For each model, the vehicle number (vehicleNr) is unique. Both attributes (modNr, vehicleNr) will be selected as the primary key. After a while, Omar noticed that he does not enjoy his databases anymore. The data modeling seems not to be well thought.

Databases I , Final Exam, January 11, 2012

Page

Exercise 4 (6 Marks) Describe the problems (redundancy and anomalies) that could occur if you use the above relational schema. Solution:
Insertion Anomaly: An insertion anomaly will occur whenever the user attempts to insert a new

model for which no vehicles are produced.

Update Anomaly: When a manufacturer's data is updated, it may occur that its information (The

modNr) will not be updated for all tuples.

Deletion Anomaly: If all cars of a specic model (or manufaturer) are deleted, all information about

the model (or the manufacturer) will be deleted.

Databases I , Final Exam, January 11, 2012

Page

Exercise 5 Assume you have the following non-trivial functional dependencies:


modNr

(2+4+2=8 Marks)

manNr, manuf, type, power

vehicleNr

manuf yearOfC, mileage, newP, saleP, purchaseP

modNr, vehicleNr

a) Explain why the relational schema is not in second normal form.


Solution:

b) The rst and second functional dependencies violates the second normal form as their right-handsides are dependant on part of the key. In the case of the rst FD, they are dependant on the modNr. In case of the second one, they are dependant on the vehicleNr c) Transform the relational schema into second normal form.
Solution:
R1(modNr, manNr, manuf, type, power) R2(vehicleNr, manuf) R3(modNr,vehicleNr, yearOfC, mileage, newP, saleP, purchaseP) ---- ------R3.modNr references R1 R3.vehicleNr references R2

d) If the transformed relational schema is not in third normalform, transform it into third normalform. Otherwise justify why it is in third normalform.
Solution: The transformed schema is in third normal form. By examining, the three functional dependencies, we nd out the following:
The L.H.S. of the rst FD is a super key in the relation R1. The L.H.S. of the second FD is a super key in the relation R2. The L.H.S. of the third FD is a super key in the relation R3.

Databases I , Final Exam, January 11, 2012

Page

Exercise 6 (5+8=13 Marks) Given the following relational schema R(A, B, C, D, E, F ) with the following functional dependencies:
B,C D A B, F

C B, D C, D B F E, F

a) Prove why {A, C} is the only candidate key.


Solution:
Compute the attribute closure of AC:

AC+ = { A, C, B, F, D, E }

Since all attributes in R are in AC+, we conclude that AC+ is a super key. Compute the attribute closure of proper subsets of AC+.
A+= { A, B, F } C+= { C, B, D, E, F}

Since niether A+ nor C+ contains all attributes in R, then AC+ is a candidate key. Compute the attribute closure of all left-hand-sides:
BC+ = { B, C, D, E, F } CD+ = { C, D, B, E, F } B+= { B, F }

Since none of them contains all attributes of R, then AC+ is the only candidate key.

Databases I , Final Exam, January 11, 2012

Page

10

b) Using the algorithm described in the lectures, transform the above relational schema into third normalform. You have to apply all steps of the algorithm starting by calculating the minimal cover.
Solution:
Compute Minimal Cover:

 BC -> D Compute BC+ = {B, C, D, E, F}. Therefore, this FD can be removed.  A -> B Compute A+ = {A, F}. Therefore, this FD can not be removed.  A -> F Compute A+ = {A, B, F}. Therefore, this FD can be removed.  C -> B Compute C+ = {C, D, E, F}. Therefore, this FD can not be removed.  C -> D Compute C+ = {C, B}. Therefore, this FD can not be removed.  CD -> E Compute CD+ = {C, D, B, F, D}. Therefore, this FD can not be removed.  CD -> F Compute CD+ = {C, D, B, E, F}. Therefore, this FD can be removed.  B -> F Compute B+ = {B, F}. Therefore, this FD can not be removed. The remaining set of FDs is G = \{ A -> B, C -> B, C -> D, CD -> E, B ->F \} Simplifying LHS for the dependency CD -> E. Check for C -> E : C+ using the given set = { C, B, D, E, F} CD+ using G = { C, D, B, E, F} The new set of FDs, X = { A -> B, C -> B, C -> D, C ->E, B -> F } Decompose the relation R:
R1(A,B) --R1.B references R3 R2(C,B,D,E) --R2.B references R3 R3(B,F)

Databases I , Final Exam, January 11, 2012

Page

11

Exercise 7 Given the following relation scheme about breakfast cereals.


cereals(CE, SE, FA, PR, TC, FI, SU, OC, CA, FC)

(8 Marks)

where CE is the primary key and the functional dependencies are:


FA, PR, TC FI, SU, OC FA FC CA TC

(Abbreviations: CE = cereal, SE = serving size, FA = fat grams, PR = protein grams, TC = total carb grams, FI = ber grams, SU = sugar grams, OC = other carb grams, CA = Calories, FC = fat Calories) Decompose this relation into a database schema in BCNF. Try to produce a scheme that preserves the FDs. Be sure to indicate clearly which tables are in your nal schema and what the key is for each table.
Solution:
R1(CE, R2(FA, R3(FI, R4(FA, SE, FA, PR, FI, SU, OC) PR, TC, CA) SU, OC, TC) FC)

Databases I , Final Exam, January 11, 2012

Page

12

Exercise 8 (5 Marks) Suppose that each functional dependency in a set F of functional dependencies on a relational scheme R satises the conditions for Third Normal Form (3NF). Suppose we use Armstrong's transitivity rule to obtain X Z from X Y and Y Z in F , where X, Y, and Z are sets of attributes of R. Show that X Z satises the conditions for 3NF in the case that neither X nor Y is a superkey. Be precise. Solution: First note that the conditions of 3NF for a functional dependency X -> Y: (1st) X is a super key. (3rd) Y is a prime attribute Suppose we use Armstrong's transitivity rule to obtain X -> Z from X -> Y and Y -> Z and neither X nor Y is a superkey. We need to show that X -> Z satises the conditions for 3NF. The 2nd condition of 3NF must hold for each of X -> Y and Y -> Z; since the 1st condition requires X and Y be superkeys, respectively. Applying the second condition to Y -> Z tells us that each attribute in Z is prime. Therefore, in the FD X -> Z, Z is a prime attribute and the FD is in 3NF.

Databases I , Final Exam, January 11, 2012

Page

13

Extra Page

Databases I , Final Exam, January 11, 2012

Page

14

Extra Page

Databases I , Final Exam, January 11, 2012

Page

15

Extra Page

You might also like