Department of Computer and Information Science Examination paper for TDT4145 Data Modelling and Database Systems Academic contact during examination: Svein Erik Bratsberg: 99539963 Roger Midtstraum: 99572420 Examination date: 6th of August 2014 Examination time (from-to): 09:00 13:00 (9am 1pm) Permitted examination support material: D No written and handwritten examination support materials are permitted. A specified, simple calculator is permitted. Other information: Language: English Number of pages: 5 Number of pages enclosed: 0 Checked by: Date Signature
Problem 1 Data Models (20 %) a) (3 %) Cardinality constraints in ER models can be expressed as shown in the diagram below. Explain the constraints that can be expressed by x, y, s and t. b) (5 %) In the ER diagram below we have shown an ER model, which use a category. Show how this ER model can be translated into a relational schema that matches the ER model as closely as possible. c) (12 %) Make an Entity-Relationship diagram for the following case. You may use all ER modelling concepts from the curriculum. I Nice you can rent city bikes. In the city centre there is a number of cycle stations with locking terminals to accommodate a number of city bikes. In order to be able to rent bikes you have to register a cellular phone number, your name and a credit card. When you want to rent a bike, you use your registered phone and call a phone number that is displayed at the locking terminal where your chosen bike is locked. If your request is granted, the bike is unlocked and you are charged for the time you have access to the bike. When you are done using the bike, you hand it in by locking the bike at a vacant locking terminal. Every bike has a unique RFID chip that is recognised by the locking terminal, which terminates your rent when the locking procedure has been completed. The company that runs the bike rental service wants to have information about where vacant bikes are locked, the bikes that are rented, the registered users, the users who have rented a bike at the moment, and all completed rentals. The database must contain the necessary information to document the users use of the bike rental service. There are different types of bikes and the system also records when a new bike is put to use in the service. State any assumptions you find necessary. Page 2 of 5
Problem 2 Relational Algabra and SQL (20 %) Use the following relational database (primary keys are underlined), which is designed to store data for a simple student-subject-exam application: Student(StudentNo, Name, Email) Subject(SubjectNo, Name) Exam(ExamNo, SubjectNo, Year, Month, Date) SubjectNo is a foreign key against the Subject table. SubjectNo is not allowed to have the NULL value. ExamRegistration(StudentNo, ExamNo, Grade) StudentNo is a foreign key against the Student table. StudentNo is not allowed to have the NULL value. ExamNo is a foreign key against the Exam table. ExamNo is not allowed to have the NULL value. Data is entered when a student registers for an exam. Grade has the NULL value until the grading process is completed. Relational algebra can be stated as text or graphs. If you master both notations we encourage you to state the answers in the graphical form. However, a correct query stated as text will get full score. a) (4 %) Make an ER model (you may use any ER construct in the curriculum) that matches the relational schema as closely as possible. State any assumptions you find necessary. b) (3 %) Write a query in relational algebra that finds StudentNo, Name and Email for all students who were registered for one of the exams, which took place on 26 th of May 2014. c) (3 %) Write a query in relational algebra that finds all students who aren t registered for any exam in 2014. In the result you should show StudentNo, Name and Email. d) (3%) Write a query in SQL that finds all subjects, which have at least one exam in 2014. The result should have columns SubjectNo and Name, there should be no duplicates in the result, and the result table should be sorted on SubjectNo in ascending order. e) (3 %) Write a query in SQL that finds the number of times the grade A has been awarded since the subject was created, in each subject, which has had one or more exams during 2014. The result table is supposed to have the columns SubjectNo, Name and Number of A. The result should be sorted on Number of A in descending order. f) (4 %) Assume that all foreign keys are defined with the constraint ON DELETE RESTRICT or ON DELETE NO ACTION (these are equal). We want to delete the student with StudentNo 100. Show how this can be done in SQL. Page 3 of 5
Problem 3 Theory (20 %) a) (4 %) Consider R = {A, B, C, D, E} and F = {AB->C, C->E, AE->B}. The closure AB + = ABCE. Explain what this means. Find all candidate keys for the table R. You must explain your answer. b) (6 %) The table R = {A, B, C D} is decomposed into R1 = {A, C} and R2 = {B, C, D}. Find a set of functional dependencies, F, which guarantees that this decomposition has the lossless-join property. Using your F, find all candidate keys and the highest normal form for the three tables R, R1 and R2. You have to justify your answer. c) (4 %) Consider R = {A, B, C, D, E} and F = {A->B, B->C, C->D, D->E}. Assume that R fulfils 1 st normal form. Find the highest normal form that is satisfied by R. You have to justify your answer. d) (6 %) Consider R = {A, B, C, D, E} and F = {A->B, B->C, C->D, D->E}. Assume that R fulfils 1 st normal form. If necessary, make a decomposition of R so that all component tables satisfy Boyce-Codd normal form (BCNF). The decomposition should have the loss-less join property, preservation of attributes and preservation of functional dependencies. You have to justify your answer. Problem 4 Extendible hashing (10 %) We are to insert the following keys in a hash file using extendible hashing in the given sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. It is space for maximum two keys in each block. Show the state of the hash file at the end. Assume there to be 4 blocks when you start inserting keys and the hash function to be h(k) = K MOD 16. Please include "global depth" and "local depth". Problem 5 Storage and query processing (15 %) Assume the following table: CREATE TABLE Employee (empno INT, lastname CHAR(30), firstname CHAR(30), email CHAR(30), startyear INT); We have the following types of storage and indexes 1. Heap file 2. Clustered B+-tree 3. Unclustered B+-tree and heap file 4. Clustered hash file 5. Unclustered hash file and heap file For each of the SQL queries below explain which storage / index type is best. When you make use of an index, describe which attribute is indexed. Explain your answers. Page 4 of 5
i) INSERT INTO Employee VALUES (12123,'Hansen','Hans','hans@email.org',2013); ii) SELECT lastname, firstname, email, startyear FROM Employee WHERE startyear=2012; iii) SELECT * FROM Employee; iv) SELECT DISTINCT lastname FROM Employee ORDER BY lastname; v) UPDATE Employee SET startyear = 2012 WHERE empno=12123; Problem 6 Transactions - SERIALIZABLE (5 %) Explain which concurrency problems we avoid by using SQL s isolation level SERIALIZABLE. Problem 7 Transactions schedule and locks (10 %) a) Look at the sequence of operations from two transactions, T1 and T2, described in the table below. What is the problem with this sequence of operations? T1 Read(X); X := X N; Write(X); Read(Y); Y := Y N; Write(Y); T2 Read(X); X := X+M; Write(X); b) By introducing conservative 2PL (two phase locking), show how the sequence of operations becomes. Page 5 of 5