Eastern Mediterranean University Department of Computer Engineering CMPE 354 – Database Management Systems Final Exam 2009 – 2010 Fall Semester January 18, 2010 Name-Surname :………………….. Student No :………………….. :………………….. Group Instructor: Assoc. Prof. Dr. Alexander CHEFRANOV TIME: 110 MINUTES. PLEASE ANSWER ALL QUESTIONS. YOU CAN BRING IN ONE A4 SIZED SHEET OF HAND-WRITTEN NOTES TO THE EXAM. PHOTOCOPIES ARE NOT ALLOWED !!! MOBILE PHONES ARE NOT ALLOWED GRADING Task 1 2 3 4 5 6 7 8 9 10 Grade TOTAL 1 A Library database shall allow answering the following Queries: 1. List the staff of the library including name, address, birthday, position, salary, enter date, the name and position of a person who has accepted him. 2. List the history of a librarian’s position/salary changes, including the date of change and name and position of a person who has approved this change. More than two changes a year are not allowed. 3. List of readers of the library including name, address, enter date 4. List of the books a reader is currently keeping, the date he took and the date to return them, the librarian’s name who has given the books. A reader is not allowed to take more than 5 books at once. 5. List the history of a reader’s book borrowings including the date of taking and returning, and the librarians’ names that have given and taken back the books. 6. List of the library books including author name, title, year of publication, publishing company, ISBN number. Task1 (10 points). Draw an E-R diagram for the Library database. It shall allow answering Queries 1-6 above. Underline primary key attributes. Id name Person enterdat e pos Addr NewPos Ch_date NewSal HistSt a Birthda yy sal IdStaff Staff WhoApp roved Accepted Is a BId enterdat e TakeDa te IdRead author year RetDate Publish er ISBN GiveReceive HistRead TakeDa te Book borro wed Reader title RetDate Bor# 2 Task 2 (10 points). Reduce the E-R diagram drawn by you in Task 1 to a relational schema. Draw it as a schema diagram (tables connected with arrows showing foreign key dependencies; each table is shown with its attributes; primary key attributes are underlined). Person Staff HistSt Id Id Id Name Ch_date Book Addr enterdat e Pos NewPos BId Birthda Salary NewSal title WhoAp c author Reader WhoAc c Id Borrow Enterdat e Reader HistRea d RId Bor# Takeda Book year Publish ISBN takedate returnda tr librarian Retdate LibrGiv e LibrRec Task 3 (10 points). Use the schema diagram drawn by you in Task 2 to write SQL DDL statements for Library database of Task 1 including primary key, foreign key, and integrity constraints. Create table person(id char(10), name char(10), addr char(15), birthday date, primary key(id)) Create table staff(id char(10), enterdate date, pos char(15), salary money, whoAc char(10), primary key(id), foreign key(id) references person, foreign key(whoAc) references staff) Create table Histst(id char(10), ch_date date, newpos char(15), newsal money, whoapp char(10), primary key(id, ch_date), foreign key(id) references staff, foreign key(whoapp) references staff) Create table book(Bid char(10), title char(10), author char(15), year date, publish char(10), ISBN char(12), primary key(Bid)) 3 Create table Reader(id char(10), enterdat date, primary key(id), foreign key(id) references person) Create table Borrow(Reader char(10), Book char(10), takedate date, returndate date, librarian char(10). primary key(reader, book, takedate), foreign key(reader) references reader, foreign key(book) references book, foreign key(librarian) references staff) Create table HistRea(Rid char(10), bor# integer, takeda date, retdate date, librgiv char(10), librrec char(10), primary key(rid, bor#), foreign key(rid) references reader, foreign key(librgiv) references staff, foreign key(librrec) references staff) Create assertion Two_changes_a_year_at_most check Not exists( Select * From histst Group by id, extract(year from ch_date) Having count(whoapp)>2 ) Create assertion Five_books_borrowed_at_once_at_most check Not exists( Select * From borrow Group by reader, takedate Having count(book)>5 ) Task 4 (10 points). Consider relational Library database defined by you in Task 3. Write SQL expressions for Queries 1 and 2. Query 1 Select p.name as p_name, p.addr, p.birthday, s.pos, s.salary, s.enterdate, b.name as boss, s2.pos as boss_pos From person p, staff s, staff s2, person b Where p.id=s.id and s.whoac=s2.id and s2.id=b.id 4 Query 2 Select p.name as p_name, h.newpos, h,newsal, h.ch_date, b.name as boss, s.pos as boss_pos From person p, histst h, staff s, person b Where p.id=h.id and h.whoapp=s.id and s.id=b.id Task 5 (10 points). Consider relational Library database defined by you in Task 3. Write relational algebra expressions for Queries 3 and 4. Query 3 name,addr,enerdat (reader person) Query 4 r.name,title,takedate,returndate,l.name( r ( person) reader id reader borrow bookbid book librarianl.id l ( person)) Task 6 (10 points). Consider relational Library database defined by you in Task 3. Write domain relational calculus expressions for Query 5. { rn, td , rd, lg, lt | (id , ad , bd )( id , rn, ad , bd person & (bor )( id , bor, td , rd, lg, lt histrea))} Task 7 (10 points). Consider relational Library database defined by you in Task 3. Write an SQL stored procedure for Query 6, taking one input parameter: author name, which may be specified by the first several first letters as ‘Smi’ , so that ‘Smith’, ‘Smiles’, etc., match it. Create procedure lib_books(in author_name) Begin Select * From book Where author like author_name||’%’ end Task 8 (10 points). Consider relational Library database defined by you in Task 3. Use SQL to create roles ‘librarian’ and ‘director’. The ‘librarian’ can register new readers and give/take books to/from readers. The ‘director’ can admit new staff members, change their position/salary, and he has full librarian’s rights. Give the role ‘director’ to Bob, and the role ‘librarian’ to Avi and Alice. Create role librarian Create role director Grant insert on reader to librarian 5 Grant insert, update, select on person to librarian Grant insert, update, select on borrow to librarian Grant insert, update, select on histrea to librarian Grant insert, update, select on person to director Grant insert, update, select on staff to director Grant insert, update, select on histst to director Grant librarian to director Grant director to Bob Grant librarian to Avi, Alice Task 9 (10 points). Give a lossless-join decomposition into BCNF of schema R=(A,B,C,D,E) if the following set F of functional dependencies holds: F={A=>BE, CD=>E, D=>B, C=>E}. Show all intermediate steps you make to come to the solution. Prove that the decomposition you get is lossless-join. A ABE R , hence, A=>BE violates BCNF condition, R1=(ABE), R2=(ACD). As far as there are no more dependencies violating BCNF condition, R1, R2 is a BCNF decomposition of R. It is lossless-join since A is a superkey for R1. Task 10 (10 points). Give a lossless-join, dependency preserving decomposition into 3NF of schema R=(A,B,C,D,E) if the following set F of functional dependencies holds: F={A=>BE, CD=>E, D=>B, C=>E}. Show all intermediate steps you make to come to the solution. Prove that the decomposition you get is lossless-join and dependency preserving. Find candidate keys: ACD is a candidate key since ACD R, AC ACBE R, AD ADBE R, CD CDEB R Finding Fc. Check F for extraneous attributes. Assume B is extraneous in A=>BE. Then F’={A=>E, CD=>E, D=>B, C=>E}. F=>F’ is trivial. Show that F’=>F. Under F’: A AE and B is not got. Hence, B is not extraneous. Assume E is extraneous in A=>BE. Then F’={A=>B, CD=>E, D=>B, C=>E}. F=>F’ is trivial. Show that F’=>F. Under F’: A AB and E is not got. Hence, E is not extraneous. Assume C is extraneous in CD=>E. Then F’={A=>BE, D=>E, D=>B, C=>E}. F’=>F is trivial. Show that F=>F’. Under F: D DB and E is not got. Hence, C is not extraneous. Assume D is extraneous in CD=>E. Then F’={A=>BE, C=>E, D=>B, C=>E}. F’=>F is trivial. Show that F=>F’. Under F: С CE and E is got. Hence, D is extraneous. 6 Hence, Fc={A=>BE, C=>E, D=>B } Using 3NF decomposition algorithm R1=(ABE) R2=(CE) R3=(DB) Since no one of the schemas contains a candidate key, R4=(ACD) R1, R4 is a lossless-join decomposition since R1 R4 A R1 R1, R4, R2 is a lossless-join decomposition since ( R1 R 4) R 2 R 2 R 2 R1, R4, R2, R3 is a lossless-join decomposition since ( R1 R 4 R 2) R3 R3 R3 The decomposition is dependency preserving since Fc Fc1 Fc2 Fc3 7