Question 1. Relational Data Model [17 marks] Question 2. SQL and Relational Algebra [31 marks]

Similar documents
How To Create A Table In Sql (Ahem)

Exercise 1: Relational Model

IT2305 Database Systems I (Compulsory)

IT2304: Database Systems 1 (DBS 1)

BCA. Database Management System

Answer Key. UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division

CSC 443 Data Base Management Systems. Basic SQL


SQL DDL. DBS Database Systems Designing Relational Databases. Inclusion Constraints. Key Constraints

Graham Kemp (telephone , room 6475 EDIT) The examiner will visit the exam room at 15:00 and 17:00.

Introduction to Database. Systems HANS- PETTER HALVORSEN,

A Brief Introduction to MySQL

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

Financial Data Access with SQL, Excel & VBA

Introduction to Databases

DBMS Questions. 3.) For which two constraints are indexes created when the constraint is added?

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

Physical Design. Meeting the needs of the users is the gold standard against which we measure our success in creating a database.

Design of Relational Database Schemas

Chapter 7: Relational Database Design

Information Systems SQL. Nikolaj Popov

Databases in Engineering / Lab-1 (MS-Access/SQL)

Database Design and Normalization

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Physical Design. Phases of database design. Physical design: Inputs.

Information Systems Modelling Information Systems I: Databases

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

Chapter 6: Physical Database Design and Performance. Database Development Process. Physical Design Process. Physical Database Design

DATABASE NORMALIZATION

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

3.GETTING STARTED WITH ORACLE8i

SQL. Short introduction

Instant SQL Programming

EECS 647: Introduction to Database Systems

There are five fields or columns, with names and types as shown above.

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Database Query 1: SQL Basics

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

DATABASE MANAGEMENT SYSTEMS. Question Bank:

B2.2-R3: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS

14 Databases. Source: Foundations of Computer Science Cengage Learning. Objectives After studying this chapter, the student should be able to:

THE BCS PROFESSIONAL EXAMINATION Diploma. October 2004 EXAMINERS REPORT. Database Systems

Relational Database Design

Entity/Relationship Modelling. Database Systems Lecture 4 Natasha Alechina

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

5.1 Database Schema Schema Generation in SQL

CPS352 Database Systems: Design Project

Database Administration with MySQL

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

Databases -Normalization III. (N Spadaccini 2010 and W Liu 2012) Databases - Normalization III 1 / 31

CIS 631 Database Management Systems Sample Final Exam

Oracle 10g PL/SQL Training

Database Design Final Project

Performance Evaluation of Natural and Surrogate Key Database Architectures

Chapter 13: Query Processing. Basic Steps in Query Processing

SAMPLE FINAL EXAMINATION SPRING SESSION 2015

C HAPTER 4 INTRODUCTION. Relational Databases FILE VS. DATABASES FILE VS. DATABASES

In This Lecture. Security and Integrity. Database Security. DBMS Security Support. Privileges in SQL. Permissions and Privilege.

Join Example. Join Example Cart Prod Comprehensive Consulting Solutions, Inc.All rights reserved.

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

CSCI-GA Database Systems Lecture 7: Schema Refinement and Normalization

Comp 5311 Database Management Systems. 16. Review 2 (Physical Level)

Intermediate SQL C H A P T E R4. Practice Exercises. 4.1 Write the following queries in SQL:

Summary on Chapter 4 Basic SQL

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. COURSE CURRICULUM COURSE TITLE: DATABASE MANAGEMENT (Code: ) Information Technology

CSI 2132 Lab 3. Outline 09/02/2012. More on SQL. Destroying and Altering Relations. Exercise: DROP TABLE ALTER TABLE SELECT

Data Analysis 1. SET08104 Database Systems. Napier University

ISM 318: Database Systems. Objectives. Database. Dr. Hamid R. Nemati

Database Design and Implementation

Lecture Notes on Database Normalization

CSE 444 Midterm Test

6 CHAPTER. Relational Database Management Systems and SQL Chapter Objectives In this chapter you will learn the following:

CMU - SCS / Database Applications Spring 2013, C. Faloutsos Homework 1: E.R. + Formal Q.L. Deadline: 1:30pm on Tuesday, 2/5/2013

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Introduction to Database Management Systems

Chapter 8. Database Design II: Relational Normalization Theory

Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst

Structured Query Language (SQL)

DBMS / Business Intelligence, SQL Server

Lecture 6. SQL, Logical DB Design

The process of database development. Logical model: relational DBMS. Relation

In this session, we use the table ZZTELE with approx. 115,000 records for the examples. The primary key is defined on the columns NAME,VORNAME,STR

Functional Dependencies and Finding a Minimal Cover

Data Modeling Basics

Advance DBMS. Structured Query Language (SQL)

Relational Database Design: FD s & BCNF

Introduction to Databases

INTRODUCTION TO DATABASE SYSTEMS

Database Design Standards. U.S. Small Business Administration Office of the Chief Information Officer Office of Information Systems Support

Performing Queries Using PROC SQL (1)

Chapter 13: Query Optimization

Databases. DSIC. Academic Year

Data Modelling and E-R Diagrams

4 Logical Design : RDM Schema Definition with SQL / DDL

Choosing a Data Model for Your Database

Information Technology NVEQ Level 2 Class X IT207-NQ2012-Database Development (Basic) Student s Handbook

Unit 2.1. Data Analysis 1 - V Data Analysis 1. Dr Gordon Russell, Napier University

Transcription:

EXAMINATIONS 2005 MID-YEAR COMP 302 Database Systems Time allowed: Instructions: 3 Hours Answer all questions. Make sure that your answers are clear and to the point. Write your answers in the spaces provided. Calculators and printed foreign language dictionaries are allowed. No reference material is allowed. There are 180 marks on the exam. CONTENTS: Question 1. Relational Data Model [17 marks] Question 2. SQL and Relational Algebra [31 marks] Question 3. Enhanced Entity Relationship Data Model [30 marks] Question 4. Mapping ER to Relational Data Model [25 marks] Question 5. Functional Dependencies and Normalization [35 marks] Question 6. Query Optimisation [26 marks] Question 7. Concurrency Control and Recovery [16 marks] Appendices: A. Formulae for Computing Query Cost Estimate B. SQL Reference 1

Question 1. Relational Data Model [17 marks] a) [3 marks] Define the structure of a relational database schema. b) [3 marks] What is the difference between a relation and a relation schema, and what is their relationship? c) [3 marks] What is a relation schema key? List the properties of a relation schema key. d) [5 marks] What is a foreign key? List the properties of a foreign key. 2

e) [3 marks] Suppose r 1 and r 2 are relations over relation schemas N 1 ({A, B}, {A}) and N 2 ({B, C}, {B}), respectively, where A is the primary key of N 1, and B is the primary key of N 2. Using the relational algebra project operator (π), express the referential integrity constraint N 1 [B] N 2 [B], and explain its meaning. 3

Question 2. SQL and Relational Algebra [31 marks] The relational database schema below is a part of a University database schema, where each student has at most one tutor and each course has at most one class representative. The schema is defined using SQL/1999 syntax. CREATE DOMAIN StudIdDomain AS int CHECK (VALUE >= 30000000 AND VALUE =< 300099999); CREATE DOMAIN CharDomain AS char(15); CREATE DOMAIN PointDomain AS int CHECK (VALUE BETWEEN 0 AND 1000); CREATE TABLE Student ( StudentId StudIdDomain PRIMARY KEY, Name CharDomain NOT NULL, NoOfPts PointDomain NOT NULL, Tutor StudIdDomain REFERENCES Student(StudentId) ); CREATE TABLE Course ( CourseId CharDomain PRIMARY KEY, CourName CharDomain, ClassRep StudIdDomain REFERENCES Student(StudentId) ); CREATE TABLE Enrolled ( StudentId StudentIdDomain NOT NULL REFERENCES Student, CourseId CharDomain NOT NULL REFERENCES Course, InTermPts PointDomain NOT NULL, PRIMARY KEY (StudentID, CourseId) ); a) Write the following SQL queries on the university database given above: i. [3 marks] Retrieve the number of students that have a tutor. 4

ii. [4 marks] Retrieve StudentId, Name and NoOfPts of all students whose Name ends with 'a', and who have NoOfPts greater than 100 and less than 300. iii. [5 marks] Retrieve StudentId and Name of all students who are not class representatives. iv. [6 marks] Retrieve StudentId and NoOfPts of all students who have more points than the student with StudentId = 007. Sort the result in descending order of NoOfPts. 5

b) Use relational algebra to write the queries below. StudentId i. [5 marks] Retrieve StudentId, Name, and InTermPts of all students who enrolled the course with CourseId =. ii. [8 marks] Retrieve StudentId and NoOfPts of all students who have more points than the student with StudentId = 007. Note: In SQL, if a nested query returns a single attribute and a single tuple, the query result will be treated as a single scalar value. Unlike SQL, Relational Algebra expressions always produce sets of tuples as outputs. Hint: You may find it useful to do this query in a stepwise way 6

(Spare Page for extra working) 7

Question 3. Enhanced Entity Relationship Data Model [30 marks] In this question you will be asked to draw EER diagrams of two external schemas for a university database. Recall that external schemas express views of different users of the possibly overlapping parts of the same mini world. Use EER notation as introduced in lectures. If you use a different notation, define it clearly in your answer. a) [12 marks] Map the part of the University relational database schema defined in Question 2 of this examination to the entity relationship data model by drawing an EER diagram. State any assumption you made. 8

9

b) [18 marks] Draw an EER diagram of the conceptual schema for another part of a University database, described as follows: Academic staff, general staff and students are the only persons at the university. Each person is either an academic staff, or a general staff, or a student. A person is uniquely identified by a PerId (person's ID), and has a Name, and an Address. An Address is composed of HouseNo, Street, and City. A characteristic property of a student is that she/he has at least one Major and one NoOfPts (number of points) for each major. An academic staff has a Position and an AcQual (academic qualification). A general staff has a GenPos (general position). An academic staff teaches at most one course, whereas a student takes at least one course. A course is uniquely identified by a CourId (course ID), and has a CourName (course name). Each course is taught by at least one academic staff, and can be taken by many students, but there may be courses that are not taken by any students. Each course can use more than one textbook, but there may be courses with no textbook. A textbook is uniquely identified by the course which uses the book, and by an OrdNo. The attribute OrdNo is the ordinal number of the book in the list of the textbooks of a particular course. A book also has a Title. 10

11

Question 4. Mapping EER to Relational Data Model [25 marks] The EER diagram on the facing page describes a part of a Bank database. Note that the entity type attributes are given separately, in the form and that entity type keys are underlined. Entity_Type_Name (Atribute 1,, Attribute n ), Each bank can have multiple accounts and loans. The accounts and loans belong to customers. a) [9 marks] Map conceptual schema Bank to a set of relation schemas S. b) [6 marks] Define the set of functional dependencies that are implied by the relation schema keys. c) [6 marks] Determine whether the set of relation schemas you produced in question 4.a) is a lossless join decomposition and explain your conclusion. If it is not a lossless join decomposition, transform it into a lossless join decomposition. 12

Bank(BankId, BankName) Bank Account(AccountNo, Balance) Loan(LoanNo, Amount) 1 1 Customer (CustomerId, Name) B_A B_L M M Account Loan N N A_C L_C M M Customer d) [4 marks] State the referential integrity constraints that have a NOT NULL foreign key. 13

Question 5. Functional Dependencies and Normalization [35 marks] a) Express the following real world facts using functional dependencies: i. [3 marks] A lecturer, identified by the value of the attribute LecturerId, has a name (Name), an office (Office), and a phone extension number (ExtensionNo). ii. [3 marks] The number of students (NoOfStud) enrolled in a course, which is identified by the value of the attribute CourseId, depends on the term when the course is offered (Term) and the year (Year). iii. [2 marks] Each office (Office) has only one phone extension number (ExtensionNo) and each phone extension number belongs to at most one office. b) [4 marks] Consider the following set of functional dependencies: F 1 = {AB C, DEL H, A C, DE H}. Transform the set F 1 into an equivalent set of fd s F 2, in which each fd is left reduced (i.e. has no redundant attributes on its left hand side). 14

c) [6 marks] Consider the following set of left reduced functional dependencies: F 3 = {AB C, C C, AC D, AB D }. Transform the set F 3 into an equivalent, non redundant set of fd s F 4. StudentId d) Consider the following third normal form relation schema: Stud_Course_Lec({StudentId, CourseId, LecturerId, Grade}, {StudentId + CourseId Grade, StudentId + CourseId LecturerId, LecturerId CourseId }). i. [3 marks] What is the set of keys K of the relation schema Stud_Course_Lec? ii. [2 marks] Which functional dependency violates BCNF? 15

iii. [5 marks] If you use the functional dependency that violates BCNF to decompose the relation schema Stud_Course_Lec into two new BCNF relation schemes, you will loose two functional dependencies and generate a pseudotransitive one. Identify the two functional dependencies that would be lost, and the pseudotransitive functional dependency that would be generated. iv. [7 marks] Use a two step procedure to decompose the relation schema Stud_Course_Lec into three BCNF relation schemas, so that you loose only one of the functional dependencies, and produce no pseudotransitive ones. In the first step take one of the functional dependencies to decompose relation schema Stud_Course_Lec into two relation schemas (one still not BCNF) without loss of any functional dependencies. Then decompose into BCNF. 16

(Spare Page for extra working) 17

Question 6. Query Optimisation [26 marks] The same part of the University database schema as in the question 2, is given below. CREATE DOMAIN StudIdDomain AS int CHECK (VALUE >= 30000000 AND VALUE =< 300099999); CREATE DOMAIN CharDomain AS char(15); CREATE DOMAIN PointDomain AS int CHECK (VALUE BETWEEN 0 AND 1000); CREATE TABLE Student ( StudentId StudIdDomain PRIMARY KEY, Name CharDomain NOT NULL, NoOfPts PointDomain NOT NULL, Tutor StudIdDomain REFERENCES Student(StudentId) ); CREATE TABLE Course ( CourseId CharDomain PRIMARY KEY, CourName CharDomain, ClassRep StudIdDomain REFERENCES Student(StudentId) ); CREATE TABLE Enrolled ( StudentId StudentIdDomain NOT NULL REFERENCES Student, CourseId CharDomain NOT NULL REFERENCES Course, InTermPts PointDomain NOT NULL, PRIMARY KEY (StudentID, CourseId) ); a) [12 marks] Draw a heuristic optimization tree that corresponds to the SQL query: SELECT StudentId, Name, InTermPts, CourName FROM (Student s NATURAL JOIN Enrolled NATURAL JOIN Course) WHERE InTermPts > 300 AND CourseId = ; 18

19

b) [14 marks] Assume: StudentId The Student relation contains data about r = 20000 students, The size of a Student tuple is L = 23 bytes, The block size is B = 500 bytes, The intermediate results of the query evaluation are materialized, The final result of the query is materialized, The size of each intermediate or final result block should not exceed 500 bytes, There is a buffer pool of 4000 bytes provided for query processing in main memory. Draw the heuristic optimization tree and calculate the lowest execution cost of the query NOTE: SELECT max(noofpts) FROM Student; Some of the formulae you may need when computing the estimated query costs are given at the end of this exam paper. 20

21

Question 7. Concurrency Control and Recovery [16 marks] a) Consider the concurrent execution of two transactions in Figure below. T i m e Transaction 1 write_lock (Y) write_lock (X) read_item (X) X = X - N write_item (X) unlock(x) Transaction 2 write_lock (X) read_item (X) X = X + M write_item (X) unlock(x) COMMIT read_item (Y) //Here, the Transaction 1 fails and //does not reach the COMMIT point Figure 7.1 (i) [2 marks] What is the name of the transaction anomaly that occurred? (ii) [5 marks] Why does the transaction anomaly in Figure 7.1 happen, and what is its consequence? 22

(iii) [3 marks] List the JDBC instructions we use to avoid transaction anomalies like one in Figure 7.1. In your answer, use the proper order of these instructions. b) Consider the diagram in Figure 7.2, and suppose the DBMS applies the immediate update recovery technique. The immediate update algorithm used here allows a transaction to commit before all of its updates are written in the database. In Figure 7.2, T i stands for transaction i, where i = 1, 2,, 5. T 1 T 2 T 3 T 4 T 5 checkpoint system failure time Figure 7.2 i. [3 marks] Which transactions will the DBMS have to REDO after the system failure? ii. [3 marks] Which transactions will the DBMS have to UNDO after the system failure? * * * * * * * * * * * * * * * * * * * 23 end

Formulae for Computing A Query Cost Estimate Blocking factor: f = B / L Number of blocks: b = r / f Selection cardinality of the attribute A : s (A) = r / d (A), where d (A) is the number of different A values Number of buffers n = K / B, where K is the size of the buffer pool C (project) = b 1 + b 2 C(project_distinct) = b 1 + b 2 + 2b 2 (1 + log m b 2-1 ) + b 2 + b 3, m = n - 1 C (select_linear) = b 1 + s(y) / f, s(y) is the selection cardinality of the search argument Y C (select_index) = x + s(y) + s(y) / f, s(y) is the selection cardinality of the indexed set of attributes Y Costs of join algorithms (o stands for the outer loop relation, and i stands for the inner loop relation) C (nested_join) = b o + b i b o / (n - 2) + js * r o * r i / f C (single_join) = b o + r o * f (index i ) + js * r o * r i / f (minimum of 4 buffers required) C (sort_join) = b 1 (3 + 2 log m b 1-1 ) + b 2 (3 + 2 log m b 2-1 ) + js * r 1 * r 2 / f, m = n - 1 C(partition_join) = 3 (b 1 + b 2 ) + js * r 1 * r 2 / f, (n - 1)( n - 2) > b 1, b 1 < b 2 C (sort) = 2b(1 + log m b - 1 ) f (index ): primary index: f (index ) = x + 1 secondary index: f (index ): x + s(y) Approximate formulae for choosing the most efficient join algorithm Mandatory condition: b o << b i (o stands for the outer loop relation, and i stands for the inner loop relation) C(single) < C(nested) r o * f (index i) < b i b o /(n 2) // Providing r o * f (index i ) >> b o C(single) < C(sort) r o * f (index i) < b i (3 + 2 log m b i - 1 ) // Providing r o * f (index i ) >> b o C(single) < C(partition-hash) r o * f (index i) < 3b i // Providing r o * f (index i ) >> b o C(nested) < C(sort-merge) b o < (n - 2)(3 + 2 log m b i - 1 ) C(nested) < C(partition-hash) b o / (n 2 ) 3 C(sort-merge) < C(partition-hash) 24 Appendix

Simplified PostgreSQL documentation: CREATE TABLE CREATE TABLE table_name ( { column_name data_type [ DEFAULT default_expr ] [ column_constraint [,... ] ] table_constraint } [,... ] ) where column_constraint is: [ CONSTRAINT constraint_name ] { NOT NULL NULL UNIQUE PRIMARY KEY CHECK (expression) REFERENCES reftable [ ( refcolumn ) ] [ ON DELETE action ] [ ON UPDATE action ] } table_constraint is: [ CONSTRAINT constraint_name ] { UNIQUE ( column_name [,... ] ) PRIMARY KEY ( column_name [,... ] ) CHECK ( expression ) FOREIGN KEY ( column_name [,... ] ) REFERENCES reftable [ ( refcolumn [,... ] ) ] [ ON DELETE action ] [ ON UPDATE action ] } and action is one of RESTRICT, CASCADE, SET NULL, or SET DEFAULT SELECT SELECT [ ALL DISTINCT ] * expression [ AS output_name ] [,...] [ FROM from_item [,...] ] [ WHERE condition ] [ GROUP BY expression [,...] ] [ HAVING condition [,...] ] [ { UNION INTERSECT EXCEPT } [ ALL ] select ] [ ORDER BY expression [ ASC DESC USING operator ] [,...] ] [ FOR UPDATE [ OF tablename [,...] ] ] where from_item can be: [ ONLY ] table_name [ * ] [ [ AS ] alias [ ( column_alias_list ) ] ] ( select ) [ AS ] alias [ ( column_alias_list ) ] from_item [ NATURAL ] [ join_type ] JOIN from_item [ ON join_condition USING ( join_column_list ) ] and join_type can be: INNER LEFT [ OUTER ] RIGHT [ OUTER ] FULL [ OUTER ] CROSS For INNER (the default) and OUTER join types, exactly one of NATURAL, ON join_condition, or USING ( join_column_list ) must appear. For CROSS JOIN, none of these items may appear. CREATE VIEW CREATE VIEW view [ ( column name list ) ] AS SELECT query Some Data Types integer, int, smallint character[n], char[n], character varying[n], varchar[n], varchar numeric, numeric[precision], numeric[precision, scale], real, double boolean, date, Note: [ xxx ] means xxx is optional, { xxx yyy } means xxx or yyy. 25 Appendix