0% found this document useful (0 votes)
94 views14 pages

Exam DBU F19 Solutions

This exam document provides instructions for a database development exam. It consists of multiple exercises to be completed in 3 hours. Students are allowed to consult books, notes, and electronic materials during the exam, but communication devices must be turned off. The exam contains 13 total pages of questions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views14 pages

Exam DBU F19 Solutions

This exam document provides instructions for a database development exam. It consists of multiple exercises to be completed in 3 hours. Students are allowed to consult books, notes, and electronic materials during the exam, but communication devices must be turned off. The exam contains 13 total pages of questions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Database Development (BAIT4/INF4/IXD6/iDA8)

Exam Assignments
Hua Lu
7th of June 2019

Full Name:

Study number:

E-mail
at student.aau.dk:

This exam consists of a number of exercises and there are three (3) hours to solve
them. When answering the questions write directly in the provided fields on this paper if
possible. Remember also to put your name and your study number on any additional
sheets of paper you will use.
• Read carefully the text of each exercise before solving it!
• Please make an effort to use a readable handwriting and to present your
solutions neatly. This will make it easier to understand your answers.
During the exam you are allowed to consult books and notes. The use of computer is
also permitted only for the electronic text book, slides, and notes! You are not allowed to
use any communication, i.e., WiFi MUST be switched off! You are not allowed to use
MySQL or any other software on your computer for helping yourself solve the exercises.

*** After this cover page, there are totally thirteen (13) pages for the exam.
Null Values [8 points in total]
A kindergarten records its children’s health information in the following table named children.
Specifically, the numeric values for height are in the unit of centimeters (cm), whereas those for
weight are in kilograms (kg). The Boolean attribute CMPA indicates whether a child has the
problem of cow’s milk protein allergy. A NULL value means the corresponding piece of
information is not available in the table. Refer to this table to answer the questions on this page.
name gender age height weight CMPA
John Male 3 96 15 TRUE
Kate Female 4 100 17 FALSE
Sebastian Male 5 110 19 NULL
Mads Male 3 100 NULL FALSE
Emil Male 5 NULL 16 FALSE
Kelly Female 4 100 15 NULL

1. Write down the names returned by each of the following SQL statements. [3 points; 1 point
each]
a. SELECT name FROM children WHERE height<=100 AND NOT CMPA;
Kate, Mads
b. SELECT name FROM children WHERE weight>15 OR CMPA;
John, Kate, Sebastian, Emil
c. SELECT name FROM children WHERE height>90 AND weight>15 AND age<5;
Kate

2. Write an SQL statement to return the names of those children who have unavailable
information on at least one of the attributes of height, weight and CMPA. [2 points]
SELECT name FROM children WHERE height IS NULL OR weight IS NULL OR CMPA IS
NULL;

3. Write down the result, in a table format, of the following SQL statement. [3 points]
SELECT name, age FROM children WHERE age >3 AND weight >15 ORDER BY name;

name age
Emil 5
Kate 4
Sebastian 5

1
Inner Join [7 points in total]
Refer to the following two tables, r and s respectively, and answer the questions on this page.

r F H s G H
a b a a
b c b b
c d c c
d d

1. What is the result of the SQL statement? (Clearly circle your choice.) [3 points]
SELECT * FROM r INNER JOIN s ON r.F = s.G;

F H G F H G H
a b b a b a a
b c c b c b b
c d d c d c d

A B

F H G H F H G H
a b a a a b a b
b c b b b c b b
c d c c c d c c

C D
2. Write down the result of the SQL statement in table format. [4 points]
SELECT F, G FROM r NATURAL JOIN s;

F G
a b
b c
c d

2
Outer Join [7 points in total]
Refer to the following two tables, r and s respectively, and answer the questions on this page.

r rid s sid value


2 1 100
3 2 200
5 3 300

1. What is the result of the following SQL statement? (Clearly circle your choice.) [3 points]
SELECT * FROM r LEFT OUTER JOIN s ON rid = sid;

A B C D
rid sid value rid sid value rid sid value rid sid value
2 2 200 2 2 200 NULL 1 100 NULL 1 100
3 3 300 3 3 300 2 2 200 2 2 200
5 NULL NULL 3 3 300 3 3 300
5 NULL NULL

2. What is the result of the following SQL statement? (Clearly circle your choice.) [4 points]
SELECT * FROM r FULL OUTER JOIN s ON rid = sid;

A B C D
rid sid value rid sid value rid sid value rid sid value
2 2 200 2 2 200 NULL 1 100 NULL 1 100
3 3 300 3 3 300 2 2 200 2 2 200
5 NULL NULL 3 3 300 3 3 300
5 NULL NULL

3
Set Operations [5 points in total]
Refer to the following three tables, u, v, and w. Answer the questions on this page.

u v w
id value id value id value
1 100 1 200 2 100
2 200 3 400 4 500
4 600

1. What is the result of the following SQL statement? (Clearly circle your choice.) [2 points]
SELECT value FROM u UNION SELECT value FROM v UNION SELECT value FROM w;
A B C D
value value value value
100 100 100 100
200 200 200 200
400 500 400 200
600 600 400
500 600
100
500

2. Write down the result of the following SQL statement in table format. [3 points]
SELECT value FROM v EXCEPT SELECT value FROM w;

value
200
400
600

4
Transaction [8 points in total]
1. About database transactions, which of the following statements are TRUE?
(There can be multiple correct answers. Clearly circle the number of each of your choices.)
[4 points; 1 point per each]
A. Given a transaction, either all its operations or none of them should take effect after the
transaction is finished.
B. A database should be always in a consistent status during the entire period of the
execution of a transaction on the database.
C. There can be many transactions being executed on the same database.
D. Once a transaction is finished successfully, the changes it makes to the database should
be durable, i.e., permanent until a future change happens.

2. The following SQL statements are executed on the table named salaries.
START TRANSACTION;
UPDATE salaries SET salary=salary*2 WHERE age>40;
COMMIT;
name age salary
Alex Jens 40 18000
Allen Wood 35 15000
Ben Hans 45 17000
Carol Jones 50 20000
Fill in the salary values for in the following table after the execution of the above SQL
statements. [4 points; 1 point per each]

name age salary


Alex Jens 40 18000
Allen Wood 35 15000
Ben Hans 45 34000
Carol Jones 50 40000

5
View and Security [8 points in total]
Consider table students that is created as follows for the relevant questions on this page.
CREATE TABLE students (
sid INT PRIMARY KEY,
age INT NOT NULL DEFAULT 22,
grade INT NOT NULL,
town VARCHAR(10));
1. View vw is created as follows.
CREATE VIEW vw AS SELECT sid, grade, town FROM students;
Which of the following INSERT statements will insert data into the table students? (Clearly
circle the numbers of your choices.) [3 points; 0.5 point each]
1. INSERT INTO vw VALUES (10, NULL, ‘Aalborg’);
2. INSERT INTO vw VALUES (20, 10, ‘Aalborg’);
3. INSERT INTO vw VALUES (30, NULL, NULL);
4. INSERT INTO vw (sid, grade, town) VALUES (40, 20, NULL);
5. INSERT INTO vw (sid, grade) VALUES (50, 10);
6. INSERT INTO vw (sid, town) VALUES (60, ‘Aalborg’);
2. Suppose Tom and Per are two database users and the following SQL statement is run
successfully:
GRANT SELECT(sid, age), DELETE ON students TO Tom WITH GRANT OPTION;
Which of the following SQL statements can Tom run successfully? [4 points; 1 point each]
1. SELECT sid FROM students WHERE age > 22;
2. SELECT * FROM students WHERE age>22;
3. GRANT DELETE ON students TO Per;
4. GRANT SELECT ON students TO Per;

3. Suppose the following SQL statement has been run successfully:


GRANT ALL ON students TO Tom WITH GRANT OPTION;
Write an SQL statement such that Tom can no longer insert any data to the table students but
still can do other operations. [1 points]
REVOKE INSERT ON students FROM Tom;

6
Joins and Subqueries [7 points in total]
Consider the following two tables about employees and departments in a university. In the employees
table, emp_id is the primary key. In the departments table, dpt_id is the primary key.
employees departments
emp_id name gender dpt_id dpt_id dpt_name location
001 Kristian Jens M 05 01 Arts SLV 300
002 Morten Nielsen M 03 02 Business FVJ 8B
003 Morten Nielsen M 01 03 Chemistry FVJ 7C
004 Sarah Jul F NULL 04 Design FVJ 7D
005 Sarah Jul F 02 05 Electronics FVJ 8B
006 Tim Jackson M 06 06 Finance SLV 300

1. How many records are there in the result of the following SQL statement? [2 points]
SELECT * FROM employees NATURAL JOIN departments;
5

2. What is the result of the following SQL statement? [2 points]


SELECT dpt_name FROM departments
WHERE location = (SELECT location FROM departments WHERE dpt_id = ‘02’);
dpt_name
Business
Electronics

3. Convert the following SQL statement to one that uses join instead of a subquery but returns
the same result. Your answer should be general enough such that even if the table contents
change the two SQL statements are still equivalent result-wise. [3 points]

SELECT e.name FROM employees e


WHERE e.name LIKE ‘%J’
AND EXISTS (SELECT * FROM departments d WHERE d.dpt_id = e.dpt_id);

SELECT name FROM employees NATURAL JOIN departments


WHERE name LIKE ‘%J’;

7
Create an ER Diagram [15 points in total]
At Free Land University (FLU), there are many scientific publications by its employees. Each
publication is given an internal serial number as its ID, and features a title, an author list with one
or many authors, an abstract, a publication venue (a journal or conference), and a publication
date. FLU is a highly self-contained university such that it does not involve any external
researchers in its publications. In other words, all publications by FLU are coauthored by internal
employees. On the other hand, one employee at FLU may coauthor an arbitrary number of
scientific publications.

Furthermore, each FLU employee is identified by his or her social security number (SSN).
Besides, FLU needs to know the name, gender, a phone number and an email address for each
employee. Also, each FLU employee is affiliated to a particular department. For each affiliation,
there is a date to indicate when the employee started working in the department. Each department
has a name, address and a web URL for its homepage. At FLU, each departments is identified
with a unique internal number.

Create an ER diagram for the requirements described above. Make it explicit which attributes
form a primary key. List any other assumptions you make.

8
Map an ER Diagram to Tables [15 points in total]
Aalborg film studio (AFS) produces a number of films every year. Each film is identified by an
internal case id, given a title, labelled with a particular genre, and produced with a fixed budget.
As a small company, AFS focuses on three genres: documentary, drama, and cartoon. To cope
with its low budget, AFS involves its employees in many film projects with different roles. For
example, John Jens, the boss of AFS, may work as the director for one film but also as a
performer for another or even the same film. There are only four kinds of roles: director,
performer, screenwriter, and cameraman. Each employee has a CPR number, name, gender, and
mobile phone number

An IT service company has created the following ER-diagram for AFS. Map the following ER-
diagram to tables using SQL CREATE TABLE statements. Remember to include all relevant
constraints such as primary and foreign keys whenever they are needed. Make sure you associate
tables through foreign keys correctly.

[Use the next page if you need more space for writing the SQL statements.]

CREATE TABLE Film (


id CHAR(10) PRIMARY KEY,
title VARCHAR(30),
genre CHAR(11) CHECK (genre IN (‘documentary’, ‘drama’, ‘cartoon’)),
budget FLOAT);

CREATE TABLE Employee (


cpr INT PRIMARY KEY, name VARCHAR(30),
gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)),
mobile_phone INT);

CREATE TABLE involves (


film_id CHAR(10) NOT NULL, emp_id INT NOT NULL,
role CHAR(11) CHECK (role IN (‘director’, ‘performer’, ‘screenwriter’, ‘cameraman’)),
FOREIGN KEY (film_id) REFERENCES Film(id),
FOREIGN KEY (emp_id) REFERENCES Employee (cpr));

9
[Use this page if you need more space for writing the SQL statements.]

10
SQL Statements Part 1 [12 points in total]
At Free Land University (FLU), all students are organized into groups each working on a
particular project supervised by one or more teachers, but only one of them can be the main
supervisor and all others are co-supervisors. A teacher, in turn, can supervise many groups. Each
group can have one or many students, and each student can be in exactly one group. The
following tables capture the relevant information for students, groups, teachers and supervisions,
respectively. Note that each table only shows a part of all its records.

Table students:
student_id name gender group_id
201416001 Alex Jensen Male sw606f16
201416002 Fie Nielsen Female dat401f16
… … …
201416109 Emilie Nielsen Female bait402f16

Table groups:
group_id title room
bait402f16 A lightweight database application for banking 3.1.20
dat401f16 Building compilers using Java 3.1.22
… … …
sw606f16 An indoor location based service application 2.1.50

Table teachers:
staff_id name gender
234578 Alexander Bernard Male
234588 Kate Madsen Female
… … …
235690 Jack Nicks Male

Table supervisions:
group_id staff_id role
bait402f16 234578 Main supervisor
bait402f16 234588 Co-supervisor
… … …
sw606f16 235690 Main supervisor

Write SQL queries to answer the following questions.

1. List the full names of all teachers who work as co-supervisor for at least one group. [2 points]
2. Get the number of students that are working on a project whose title contains ‘Java’ or ‘database’.
[2 points]
3. List the number of groups supervised by female and male main supervisors, respectively. [4 points]
4. List the complete information of all students who are supervised by a female main supervisor.
[4 points]

11
1. SELECT name FROM teachers NATURAL JOIN supervisions WHERE role=’Co-
supervisor’;

2. SELECT COUNT(name) FROM students NATURAL JOIN groups WHERE title LIKE
‘%Java%’ OR title LIKE ‘%database%’;

3. SELECT gender, COUNT(group_id) FROM teachers NATRUAL JOIN supervisions WHERE


role=’Main supervisor’ GROUP BY gender;

4. SELECT students.* FROM (students NATURAL JOIN supervisions) INNER JOIN teachers ON
supervisions.staff_id=teachers.staff_id WHERE teachers.gender = ‘Female’ AND role=’Main
supervisor’;

12
SQL Statements Part 2 [8 points in total]
SLV300 High School surveyed the height and weight of their final year students. A part of the result is
listed in the following table named survey. The height column is in the unit of cm and the weight column
is in the unit of kg.

name gender age weight height

Tim Larsen Male 20 73 177

Alex Wind Male 18 81 183

Laura Thomsen Female 19 55 169

Sara Poulsen Female 19 54 168

Pia Nielsen Female 18 59 173

Lars Jensen Male 17 75 176

Ann Olesen Female 18 62 170

Mads Madsen Male 19 72 176

1. Write an SQL statement to return the minimum height and minimum weight of each age group.
[3 points]

SELECT age, MIN(height), MIN(weight) FROM survey GROUP BY age;

2. Write an SQL statement to return the complete information of all students who are taller than the
shortest by more than 20cm in their age group. Make sure the records in the result table are sorted
ascendingly on the height column. [5 points]

SELECT * FROM survey NATURAL JOIN (SELECT age, MIN(height) AS min_height FROM
survey GROUP BY age ) AS x WHERE (survey.height - x.min_weight > 20) ORDER BY
survey.height ASC;

13

--- End of Exam Paper ---

You might also like