Eastern Mediterranean University Department of Computer Engineering 143 CMPE 354 – Database Management Systems Final Exam 2013 – 2014 Fall Semester January 24, 2014 Name-Surname :………………….. Student No :………………….. Group :………………….. Instructor: Assoc. Prof. Dr. Alexander CHEFRANOV TIME: 150 MINUTES. MOBILES ARE NOT ALLOWED!!! THERE ARE TEN QUESTIONS. EACH QUESTION IS 10 POINT. PLEASE ANSWER ALL QUESTIONS. YOU CAN BRING IN ONE A4 SIZED SHEET OF HAND-WRITTEN NOTES TO THE EXAM. PHOTOCOPIES ARE NOT ALLOWED !!! Consider the following database Employee(empno, name, office, age) Books(isbn, title, authors, publisher) Loan(empno, isbn, date) Primary keys are underlined, foreign keys are used. The database will be used in the questions below 1. (10 points) Build Entity-Relationship diagram for the database. Specify mapping cardinality constraints, participation constraints. Explain how the constraints are reflected in the ER diagram. Discuss the attributes (atomic, multi-valued, composite, derived). Change, if necessary, some attributes; give reasons for that. Employee books loan empno isbn date title name publisher office birthday y authors age Relation loan is binary many-to-many, with partial participation of books and Employee. Authors is a multivalued attribute, age is a derived from birthday attribute 2 2. (10 points) Represent the database by the tabular schema diagram where each table is represented by a box with attributes inside it, primary keys are underlined, and foreign keys are shown by arrows directed from a referencing table to the referenced one. Hint: Some attributes may need a separate table Employee Loan books Empno Empno isbn name isbn title office publisher Authors author isbn birthday 3 3. (10 points) Write a relational algebra expression for the following query: Find names of employees who borrowed more than three books published by Addison Wesley. name (employee c _ tit3 ( X ( empno,c _ tit) ( empno Gcount(title) (loan publisher ' AW ' (books))))) 4. (10 points) Write a relational algebra expression for the following query: Find titles of the books with more than two authors. Hint: You may need a separate table for authors attribute in the database title (books c _ auth 2 ( isbn Gcount( author) as c _ auth (authors))) 4 5. (10 points) Write an SQL expression for the following query: Find names of employees who borrowed more than three books published by Addison Wesley. Select name From employee Where empno in ( Select empno From loan Where isbn in (select isbn from books where publisher=’AW’) Group by empno Having count(*)>3 ) 6. (10 points) Write an SQL expression for the following query: Find titles of books with more than two authors. Hint: You may need a separate table for authors attribute in the database Select title From books Where isbn in ( Select isbn From authors Group by isbn Having count(*)>2 ) 5 7. (10 points) Consider the C code below: Char*sqlprog=”update account set balance=balance*1.05 where account.number=?”; Sqlprog is an array containing SQL query of increasing balance of an account specified as a parameter of that query EXEC SQL prepare dynprog from :sqlprog; SQL query is prepared for launching as dynprog from the program variable sqlprog Char account[10]=”A-101”; account is a 10-element array initialized by “A-101” EXEC SQL execute dynprog using :account; SQL query from dynprog is invoked with parameter from the program variable account Explain the meaning of the code as a whole and each its line in particular. The code as a whole invokes an SQL query increasing by 5% balance of account A-101. 6 8. (10 points) Write a function in SQL with one input: Publisher. The function returns names of employees who borrowed more than three books published by the Publisher Hint: Consider the code below: Create function accounts_of(customer_name char(20)) Returns table(account_number char(10), Branch_name char(15), Balance numeric(12,2)) Return table( Select account_number, branch_name, balance From account Where exists( Select * From depositor Where depositor.customer_name=accounts_of.customer.name And depositor.account_number=account.account_number) ) Adjust the code above to solve your problem Create function emp_names(Publisher char(20)) Returns table(name char(10) ) Return table( Select name From employee Where empno in ( Select empno From loan Where isbn in ( select isbn from books b where b.publisher=emp_names.Publisher ) Group by empno Having count(*)>3 ) ) 7 9. (10 points) Consider the code below Create trigger reorder_trigger after update of amount on inventory Reorder_trigger is defined that is invoked after update of amount attribute of inventory relation Referencing old row as orow, new row as nrow Orow contains modified tuple value before update, nrow contains modified tuple value after update For each row For each modified tuple to be done When nrow.level<=(select level from minlevel where minlevel.item=orow.item) If level of the item after update is less or equal to the value shown for that item in minlevel table And orow.level>(select level from minlevel where minlevel.item=orow.item) And if before update level of the item was greater than the value shown for that item in the minlevel table then Begin Insert into orders Into orders relation (select item, amount Item and amount are inserted From reorder Taken from reorder table Where reorder.item=orow.item) Where item value matches to that used in the modified tuple referred to by orow end Explain functionality of the code as a whole and each its line in particular. The trigger is invoked after update of amount attribute of inventory relation. It checks whether the current level of an item falls below the specified minimal value, and if so, it inserts into orders relation amount of the item necessary to purchase taken from the reorder relation. 8 10. (10 points) Write an SQL expression to give the users Bob and Alice privileges on update authors in books and insertion into loan. Grant update(authors) on books, insert on loan to Bob, Alice 9