COMP 302 Database Systems - Victoria · PDF fileCOMP 302 Database Systems Time allowed: ......
Transcript of COMP 302 Database Systems - Victoria · PDF fileCOMP 302 Database Systems Time allowed: ......
EXAMINATIONS — 2007
MID-YEAR
COMP 302
Database Systems
Time allowed: 3 Hours
Instructions: Answer all questions.
Make sure that your answers are clear and to the point.
Calculators and printed foreign language dictionaries are allowed.
No reference material is allowed.
There are 180 marks on the exam.
Marks are shown for each question as a whole and also for their parts.
CONTENTS:
Question 1. Database Basics [20 marks]
Question 2. SQL and Relational Algebra [40 marks]
Question 3. Query Optimisation [20 marks]
Question 4. Enhanced Entity Relationship Data Model [25 marks]
Question 5. Mapping ER to Relational Data Model [20 marks]
Question 6. Functional Dependencies and Normalization [40 marks]
Question 7. Transaction Processing [15 marks]
Appendices:
A. SQL Reference
B. Student Database Schema
C. An Instance of the Student Database
D. EER Diagram
StudentId_____________
COMP302 continued 2
Question 1. Database Basics [20 marks]
a) [5 marks] Define the term database and identify its main features.
ANSWER
b) [2 marks] Define the term relational database schema (the schema of a whole relational
database).
ANSWER
c) [3 marks] Define the term relation schema key. List the properties of a relation schema key.
ANSWER
StudentId_____________
COMP302 continued 3
d) [5 marks] Define the term foreign key. List the properties of a foreign key.
ANSWER
e) [2 marks] What is a data manipulation language used for?
ANSWER
f) [3 marks] List the components (sublanguages) of the relational language SQL?
ANSWER
StudentId_____________
COMP302 continued 4
Question 2. SQL and Relational Algebra [40 marks]
Consider the Student database schema given below.
CREATE TABLE Student ( StudentId int PRIMARY KEY, Stud_Name varchar NOT NULL); CREATE TABLE Course ( CourseId char(7) PRIMARY KEY, Cour_Name varchar NOT NULL, NoOfPts int NOT NULL); CREATE TABLE Enrolled ( StudentId int NOT NULL REFERENCES Student, CourseId char(7) NOT NULL REFERENCES Course, Grade char(2), PRIMARY KEY (StudentId, CourseId)); CREATE TABLE Stud_Sport ( StudentId int NOT NULL REFERENCES Student, Sport_Name varchar NOT NULL, PRIMARY KEY (StudentId, Sport_Name));
Note: The same relational database schema is also given in the Appendix B to this exam paper. You
may find it convenient to tear it off and use when answering the following questions.
a) [14 marks] Write SQL queries for the database above to do the following:
i. [5 marks] Retrieve names of students that have enrolled the course with the name Database
Systems.
ANSWER
StudentId_____________
COMP302 continued 5
ii. [4 marks] Use either substring(string [from int] [for int]) function, or
LIKE expression, or SIMILAR TO operator to retrieve students whose name start with
capital letter S.
ANSWER
iii. [5 marks] Retrieve student id's and student names of students that passed at least one course
with a grade better than B+ (i.e. A-, A, or A+). Avoid duplicates.
ANSWER
StudentId_____________
COMP302 continued 6
b) [8 marks] Use relational algebra to write the following queries:
i. [5 marks] Retrieve names of students that have enrolled in Database Systems.
ANSWER
ii. [3 marks] Retrieve student id's and student names of students that passed at least one course
with a grade better than B+ (i.e. A-, A, or A+). Avoid duplicates.
ANSWER
StudentId_____________
COMP302 continued 7
(Spare Page for extra working)
StudentId_____________
COMP302 continued 8
An instance of the Student database schema above is given below and in Appendix C.
c) [10 marks] Consider the SQL statement below.
q2db=> SELECT s.StudentId, Stud_Name
FROM Student s
WHERE NOT EXISTS
((SELECT e.StudentId
FROM Enrolled e
WHERE s.Studentid = e.StudentId)
EXCEPT
(SELECT p.StudentId
FROM Stud_Sport p
WHERE Sport_Name = 'Squash' AND
s.Studentid = p.StudentId));
studentid | stud_name -----------+-------------- 7007 | James Bond 1313 | Susan Brown 5050 | Susan Smith 8989 | Megan Black
Student Table
courseid | cour_name | n oofpts ----------+-------------------------------------+-- ------- COMP302 | Database Systems | 15 COMP203 | Computer Organization | 22 COMP206 | Program and Data Structures | 22 COMP442 | Issues in Database and Info Systems | 15
studentid | courseid | grade ----------+----------+------- 7007 | COMP203 | A+ 7007 | COMP206 | A+ 7007 | COMP302 | A+ 7007 | COMP442 | 1313 | COMP203 | A 1313 | COMP302 | B+ 5050 | COMP203 | C+ 1313 | COMP442 |
studentid | sport_name ----------+-------------- 1313 | Netball 1313 | Squash 5050 | Netball 8989 | Netball 1313 | Table Tennis 7007 | Rugby 7007 | Squash
Course Table
Enrolled Table Stud_Sport Table
StudentId_____________
COMP302 continued 9
i. [8 marks] Explain in English what the statement means.
ANSWER
ii. [2 marks] What will the statement return if executed against the database instance
given above?
ANSWER
StudentId_____________
COMP302 continued 10
d) [8 marks] Consider the following relational algebra expression
(StudentId, Stud_Name)FFFF(SUM NoOfPts)(Student*Enrolled*Course)
i. [5 marks] What would the relational algebra expression return if executed against the
database instance given above?
ANSWER
ii. [3 marks] Write an equivalent SQL statement.
ANSWER
StudentId_____________
COMP302 continued 11
(Spare Page for extra working)
StudentId_____________
COMP302 continued 12
Question 3. Query Optimisation [20 marks]
a) [10 marks] Consider the Student database schema given in Question 2 and Appendix B. Draw
the heuristic optimization tree that corresponds to the following SQL query:
SELECT Stud_Name FROM Student NATURAL JOIN Enrolled NATURAL JOIN Course WHERE Cour_Name = 'Database Systems';
ANSWER
StudentId_____________
COMP302 continued 13
b) [10 marks] Consider the SQL query
q2db=> SELECT s.StudentId, Stud_Name
FROM Student s
WHERE NOT EXISTS
((SELECT e.StudentId
FROM Enrolled e
WHERE s.Studentid = e.StudentId)
EXCEPT
(SELECT p.StudentId
FROM Stud_Sport p
WHERE Sport_Name = 'Squash' AND
s.Studentid = p.StudentId));
This is the same query you considered in Question 2 c). This query is not only logically complex,
but also very inefficient.
i. [5 marks] Justify the claim that that the query above is very inefficient.
ANSWER
ii. [5 marks] Write a SQL query that will produce the same result as the query above, but that
run more efficiently. Explain why you think your query will run more efficiently.
ANSWER
StudentId_____________
COMP302 continued 14
Question 4. EER Data Model [25 marks]
Consider the EER diagram on the facing page. Redraw the diagram to include the following
structural constraints:
• A division operates many departments, but each department is operated by only one division.
Each division must have at least one department and each department must belong to a division.
• A department employs many employees, but there may be a department with no employees.
• Majority of employees called “dept_emps” are employed by a department. However, there are
also employees known as “free_lance_emps” who are not assigned to any department.
• A department may own many projects, but there may be a department with no projects. Each
project is existentially and identification dependent on only one department.
• Both dept_emps and free_lance_emps can work on many projects, but there are employees who
do not work on any project. A project has at lest one employee working on it.
• One of dept_emps ménages a department. Each department must have a manager.
• One of the employees runs a division. Each division must have someone running it.
Note: The same EER diagram is also given in the Appendix D to this exam paper. You may find it
convenient to tear it off and use when answering this question.
StudentId______________
COMP302 continued 15
Division Department
Employee
Project
Works_On
Operates
Employs Manages
Runs
Owns
StudentId______________
COMP302 continued 16
ANSWER
StudentId_______________________
COMP302 continued 17
(Spare Page for extra working)
StudentId_______________________
COMP302 continued 18
Question 5. Mapping ER to Relational Data Model [20 marks]
In this question you will be asked to map a number of ER diagrams into relational database schemas
(S, I ), where S is a set of relation schemas, and I is a set of referential integrity constraints. The ER
diagrams will contain only symbols for entity and relationship types, and relationship constraints.
Attributes will be given separately in the form
Entity_Type_Name (Attribute1,…, Attributen),
with entity type keys underlined, or
Relationship_Type_Name (Attribute1,…, Attributem),
where the set of a relationship attributes can be empty.
If an attribute A is multivalued, and its values are tuples of the form (b1,…, bn), it is denoted using
the following notation
A{(B1,…, Bn)},
where Bi, 1 ≤ i ≤ n, are attributes, and bi∈dom(Bi).
Represent your relation schema in the form
N(R, K),
where R is the set of attributes and K is the set of keys of the relation schema with the name N. To
indicate that a foreign key has to be not null, use the following notation
Null (Relation_Schema_Name, Attribute) = Not.
a) [5 marks] Map the following ER diagram into a relational database schema.
Lecturer Department Works_For
N 1
Lecturer (LecturerId, Lec_Name, Position)
Works_For ( )
Department (DeparmentId, Dep_Name)
StudentId_______________________
COMP302 continued 19
ANSWER
b) [7 marks] Map the following ER diagram into a relational database schema.
ANSWER
Student (StudentId, St_Name, Exams{(CourseId, Grade)})
Student
StudentId_______________________
COMP302 continued 20
c) [8 marks] Map the following ER diagram into a relational database schema. Include all
constraints you think should be satisfied.
ANSWER
Course(CourseId, Cour_Name, NoOfPts )
Prerequisite( )
Course Prerequsite is_a_prerequsite_of
has_prerequsites
N
N
StudentId_______________________
COMP302 continued 21
(Spare Page for extra working)
StudentId_______________________
COMP302 continued 22
Question 6. Functional Dependencies and Normalization [40 marks]
a) [5 marks] Consider a relation schema N (R, K), where R = {A, B, C} and K = {AB} (AB is the
primary key). Define a set of functional dependencies F under which N is in the first but not in
the second normal form.
ANSWER
b) [5 marks] Consider a relation schema N (R, K), where R = {A, B, C, D} and K = {AB} (AB is
the primary key). Define a set of functional dependencies F under which N is in the second but
not in the third normal form.
ANSWER
StudentId_______________________
COMP302 continued 23
c) [5 marks] Consider a relation schema N (R, K), where R = {A, B, C} and K = {AB} (AB is the
primary key). Define a set of functional dependencies F under which N is in the third but not in
the BCNF normal form.
ANSWER
d) [5 marks] Consider a relation schema N (R, K), where R = {A, B, C} and K = {AB} (AB is the
primary key). Define a set of functional dependencies F under which N is in the BCNF normal
form.
ANSWER
StudentId_______________________
COMP302 continued 24
e) [20 mark] Suppose you are given a relation schema N (R, K ), where R = {A, B, C, D} and K is
the set of relation schema keys. For each of the following sets of functional dependencies,
assuming those are the only functional dependencies that hold for N, do the following:
1. Identify all keys for N.
2. Identify the highest normal form that N satisfies (e.g. 1NF, 2NF, 3NF, BCNF).
3. If N is not in 3NF, transform it into a set of at least 3NF relation schemas that
preserve attributes and functional dependencies and have a lossless join property.
4. Check whether your decomposition satisfies BCNF normal form.
i. [10 marks] F = {A→B, BC→D, A→C}.
ANSWER
ii. [10 marks] F = {B→C, D→A}.
ANSWER
StudentId_______________________
COMP302 continued 25
(Spare Page for extra working)
StudentId_______________________
COMP302 continued 26
Question 7. Transaction Processing and Concurrency Control [15 marks]
Consider the code fragments of a Java JDBC program on the facing page. Note that “…” after a
comment is in place of the code that implements the action of the comment.
a) [3 marks] Which commands determine transaction boundaries?
ANSWER
b) [3 marks] Which command performs actual locking in the book table and what is locked?
ANSWER
c) [3 marks] What will be the content of the res object if the command res.next(); returns
true?
ANSWER
d) [3 marks] Why is a try catch pair of blocks needed inside the catch block at the end of
the code?
ANSWER
e) [3 marks] What is the task of the con.setAutoCommit(false) command?
ANSWER
StudentId_______________________
COMP302 27 end
. . .
Connection con;
int j_isbn;
try {
// Establish a connection with a database
. . .
con.setAutoCommit(false);
// Do some processing
. . .
PreparedStatement retBook = con.prepareStatement("SELECT * FROM book WHERE isbn=? FOR UPDATE");
// Obtain data for the variable j_isbn
. . .
retBook.setInt(1, j_isbn);
ResultSet res = retBook.executeQuery();
if (res.next()) {
// Do some processing
. . .
// Do some database updating
. . .
}
con.commit();
con.setAutoCommit(true);
}
catch (SQLException ex) {
try {
con.rollback();
con.setAutoCommit(true);
}
catch (SQLException e) {
}
return "\nSQLException: " + ex.getMessage();
}
// Close connection and exit
. . .
*********
COMP302 Appendix 28
APPENDIX A
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.
COMP302 Appendix 29
APPENDIX B
Student Database Schema
CREATE TABLE Student ( StudentId int PRIMARY KEY, Stud_Name varchar NOT NULL); CREATE TABLE Course ( CourseId char(7) PRIMARY KEY, Cour_Name varchar NOT NULL, NoOfPts int NOT NULL); CREATE TABLE Enrolled ( StudentId int NOT NULL REFERENCES Student, CourseId char(7) NOT NULL REFERENCES Course, Grade char(2), PRIMARY KEY (StudentId, CourseId)); CREATE TABLE Stud_Sport ( StudentId int NOT NULL REFERENCES Student, Sport_Name varchar NOT NULL, PRIMARY KEY (StudentId, Sport_Name));
COMP302 Appendix 30
APPENDIX C
An Instance of the Student Database Schema
studentid | stud_name -----------+-------------- 7007 | James Bond 1313 | Susan Brown 5050 | Susan Smith 8989 | Megan Black
Student Table
courseid | cour_name | n oofpts ----------+-------------------------------------+-- ------- COMP302 | Database Systems | 15 COMP203 | Computer Organization | 22 COMP206 | Program and Data Structures | 22 COMP442 | Issues in Database and Info Systems | 15
studentid | courseid | grade ----------+----------+------- 7007 | COMP203 | A+ 7007 | COMP206 | A+ 7007 | COMP302 | A+ 7007 | COMP442 | 1313 | COMP203 | A 1313 | COMP302 | B+ 5050 | COMP203 | C+ 1313 | COMP442 |
studentid | sport_name ----------+-------------- 1313 | Netball 1313 | Squash 5050 | Netball 8989 | Netball 1313 | Table Tennis 7007 | Rugby 7007 | Squash
Course Table
Enrolled Table Stud_Sport Table
COMP302 Appendix 31
APPENDIX D
EER Diagram
Division Department
Employee
Project
Works_On
Operates
Employs Manages
Runs
Owns