COMP 302 Database Systems
COMP 302 Database Systems
MID-YEAR
COMP 302
Database Systems
CONTENTS:
Appendices:
A. SQL Reference
B. Student Database Schema
C. An Instance of the Student Database
D. EER Diagram
StudentId_____________
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
COMP302 2 continued
StudentId_____________
d) [5 marks] Define the term foreign key. List the properties of a foreign key.
ANSWER
ANSWER
ANSWER
COMP302 3 continued
StudentId_____________
Question 2. SQL and Relational Algebra [40 marks]
Consider the Student database schema given below.
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
COMP302 4 continued
StudentId_____________
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
COMP302 5 continued
StudentId_____________
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
COMP302 6 continued
StudentId_____________
(Spare Page for extra working)
COMP302 7 continued
StudentId_____________
An instance of the Student database schema above is given below and in Appendix C.
Student Table
studentid | stud_name
-----------+--------------
7007 | James Bond
1313 | Susan Brown
5050 | Susan Smith
8989 | Megan Black
Course Table
courseid | cour_name | noofpts
----------+-------------------------------------+---------
COMP302 | Database Systems | 15
COMP203 | Computer Organization | 22
COMP206 | Program and Data Structures | 22
COMP442 | Issues in Database and Info Systems | 15
COMP302 8 continued
StudentId_____________
ii. [2 marks] What will the statement return if executed against the database instance
given above?
ANSWER
COMP302 9 continued
StudentId_____________
F
(StudentId, Stud_Name) (SUM NoOfPts)(Student*Enrolled*Course)
i. [5 marks] What would the relational algebra expression return if executed against the
database instance given above?
ANSWER
COMP302 10 continued
StudentId_____________
(Spare Page for extra working)
COMP302 11 continued
StudentId_____________
SELECT Stud_Name
FROM Student NATURAL JOIN Enrolled NATURAL JOIN Course
WHERE Cour_Name = 'Database Systems';
ANSWER
COMP302 12 continued
StudentId_____________
b) [10 marks] Consider the SQL query
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
COMP302 13 continued
StudentId_____________
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.
COMP302 14 continued
StudentId______________
Employs Manages
Runs Employee
Works_On
Project Owns
COMP302 15 continued
StudentId______________
ANSWER
COMP302 16 continued
StudentId_______________________
COMP302 17 continued
StudentId_______________________
N 1
Lecturer Works_For Department
COMP302 18 continued
StudentId_______________________
ANSWER
Student
ANSWER
COMP302 19 continued
StudentId_______________________
c) [8 marks] Map the following ER diagram into a relational database schema. Include all
constraints you think should be satisfied.
is_a_prerequsite_of N
Prerequsite Course
N
has_prerequsites
ANSWER
COMP302 20 continued
StudentId_______________________
COMP302 21 continued
StudentId_______________________
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
COMP302 22 continued
StudentId_______________________
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
COMP302 23 continued
StudentId_______________________
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
COMP302 24 continued
StudentId_______________________
COMP302 25 continued
StudentId_______________________
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
COMP302 26 continued
StudentId_______________________
. . .
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 27 end
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 28 Appendix
APPENDIX B
COMP302 29 Appendix
APPENDIX C
Student Table
studentid | stud_name
-----------+--------------
7007 | James Bond
1313 | Susan Brown
5050 | Susan Smith
8989 | Megan Black
Course Table
courseid | cour_name | noofpts
----------+-------------------------------------+---------
COMP302 | Database Systems | 15
COMP203 | Computer Organization | 22
COMP206 | Program and Data Structures | 22
COMP442 | Issues in Database and Info Systems | 15
COMP302 30 Appendix
APPENDIX D
EER Diagram
Employs Manages
Runs Employee
Works_On
Project Owns
COMP302 31 Appendix