Mindterm Sol
Mindterm Sol
Printed lecture notes, personal notes, textbook and other course handout
materials.
No Electronic devices.
VENUE
STUDENT ID
SEAT NO
NAME
1
Problem ONE: ER Diagram [20 points]
Consider a university research project management system consisting of the following entities:
(a) Researcher, having a unique ORCID, and other attributes: name, sex, titles.
(b) Department, having a unique department name, and other attributes: department location, total
number of researchers belongs to the department.
(c) Publication, having a unique DOI, and other attributes: title, date of publication, journal/conference
name.
(d) Project, with attributes: project title, start date, end date, duration of the project.
Answer:
2. Draw the ER diagram for the entity type Researcher, assume each Researcher could have multiple
different titles, and each title is composed by the title name and the award year. [4 points]
Answer:
3. Assume each Researcher only work for one Department, and each Department has many Researchers,
among them one Researcher works as the head of the department. A Researcher can publish no or
many Publications, and each Publication could have one or multiple authors (Researchers).
Additionally, a Publication could cite or be cited by no or many Publications. Draw the ER diagram
2
for relationships between Researcher, Department and Publication. (No need to draw the attributes
of each entity). [8 points]
Answer:
4. Assume each Project is led by one Researcher, and a Researcher can lead no or many Projects. Different
Researchers may have Projects with the same title, but the same Researcher will not have two Projects
with the same title. Draw the ER diagram for the entity type Project and its relationship to the entity type
Researcher. (No need to draw the attributes of Researcher.) [5 points]
Answer:
3
Note: you can define a relation in the sample format below:
Answer:
4
2. How many primary keys, candidate keys, and superkeys are there for the relation 'Branch’? [4
points]
Answer:
It has 1 primary key (1 points), 2 candidate key (1 points) and 12 superkeys (2 points)
3. Assuming that the tables for the entities 'Bank,' 'Branch,' 'Loan,' 'Customer,' and 'Library' already
exist, please create necessary tables to represent the entity 'Account' and the relationship 'Apply'
using SQL statements, while defining the primary keys and foreign keys. (Hint: you can define the
datatype of attributes by yourself). [9 points]
Answer:
Customer
Customer_id Name Gender Birth_date Address
BookOrder
Order_id Customer_id Book_id Quantity
101 1 1001 2
102 3 1002 5
103 2 1001 1
104 1 1003 1
105 3 1002 4
Book
6
Book_id Title Author Price
(1) Analyze the primary keys of each table based on the application. [3 points]
Answer:
For the Customer table: Customer_id
For the BookOrder table: Order_id
For the Book table For the Book table: (Book_id, Title)
(2) Suppose all three tables are already created, write corresponding SQL statements to define all
foreign keys. [4 points]
(3) For 3.1 and 3.2 below, suppose each of the following operations is applied directly to the database.
Discuss all integrity constraints violated by each operation if any, and the different ways of enforcing
these constraints.
3.1) Delete tuple <1002, "AI for Everyone", Andrew Ng, $35.99> from Book table. [4 points]
Answer:
Violates the referential integrity constraint because multiple existing tuples in BookOrder
refer to this book tuple. If the tuple is deleted, these BookOrder entries will refer to a non-
existent book.
3.2) Insert <null, ‘Charlie’, ‘Male’, 1989, ‘452 Maple St. Plainview’> into Customer. [4 points]
7
Answer:
Violates the entity integrity constraint because Customer_id is a primary key and
cannot be NULL.
Violates the domain constraint because Birth_date is not stored in a valid date format;
it should not be an integer.
2 Given a relation schema R (A,B,C,D,E) with the function dependency set F={ AB→D,
AC→B, D→E, E→A}, please determine whether each of the following functional
dependency is in F+. (Hint: no need to show the proof.) [5 points]
1) BE→D
2) AC→E
3) BC→D
4) DE→B
5) AC→D
Answer:
1. AB (given)
2. BE (given)
3. AE (transitivity by 1&2)
4. CHD (given)
5. CH (decomposition rule by 4)
6. AC EC (augmentation rule by 3)
7. EC EH (augmentation rule by 5)
8. AC EH (transitivity by 6&7)
9. ACG AC (reflective rule by 8)
10. ACG EH (transitivity by 8&9)
8
2. Let’s consider the following relationship R storing the information about concert.
R(ConcertID, VenueNo, VenueName, Performer, ConcertDate, StageNo, LocationID, StageCapacity,
ManagerNo)
It has following functional dependencies:
ConcertID → VenueNo
VenueNo → VenueName
{ConcertID, Performer, ConcertDate} → {StageNo, LocationID, ManagerNo}
{StageNo, LocationID, ConcertDate} → StageCapacity
(2) Is the relation R in 2NF and why? If not, decompose it into Two tables which satisfy 2NF. [5
Points]
Answer: Not in 2NF, because there exists partial function dependency on primary key, ConcertID →
VenueNo and VenueNo → VenueName
2NF decomposition:
R1 (ConcertID, VenueNo, VenueName)
R2 (ConcertID, Performer, ConcertDate, StageNo, LocationID, ManagerNo, StageCapacity)
(3) Does your decomposition in (2) satisfy 3NF and why? If not, normalize it into 3NF. [5 Points]
Answer: Not in 3NF, because there exists transitive function dependency on primary key: ConcertID
→ VenueNo → VenueName in R1 and {StageNo, LocationID, ConcertDate} → StageCapacity in R2.
3NF decomposition:
R1A (ConcertID, VenueNo)
R1B (VenueNo, VenueName)
R2A (ConcertID, Performer, ConcertDate, StageNo, LocationID, ManagerNo)
R2B (StageNo, LocationID, ConcertDate, StageCapacity)
(4) Does your decomposition in (3) satisfy BCNF and why? If not, normalize it into BCNF. [3 Points]
Answer: Yes, it already satisfies BCNF. Because in each table, for each functional dependency, the
left-hand side is a super key.
Given the following relations about the information of an online shopping system. [20 points]
Suppose now we have a valid database state. Answer the following questions by completing
the missing parts of the given SQL statements.
(1) List the Name and Price of all products in the 'Electronics' category that cost more than 500.
[4 points]
SELECT _______________________________________________________
FROM _________________________________________________________
WHERE ________________________________________________________;
Answer:
SELECT Name, Price
FROM Product
WHERE Category = 'Electronics' AND Price > 500;
(2) List the CustomerID and Name of customers who have ordered products from both
'Electronics' category and 'Books' category. [4 points]
(SELECT _______________________________________________________
FROM _________________________________________________________
WHERE ________________________________________________________ )
________________________________________________________________
(SELECT _______________________________________________________
FROM _________________________________________________________
WHERE ________________________________________________________);
Answer:
(SELECT DISTINCT C.CustomerID, C.Name
FROM Customer AS C, Order AS O, OrderItem AS OI, Product AS P
WHERE C.CustomerID = O.CustomerID AND O.OrderID = OI.OrderID AND OI.ProductID
= P.ProductID AND P.Category = 'Electronics')
INTERSECT
(SELECT DISTINCT C.CustomerID, C.Name
FROM Customer AS C, Order AS O, OrderItem AS OI, Product AS P
WHERE C.CustomerID = O.CustomerID AND O.OrderID = OI.OrderID AND OI.ProductID
10
= P.ProductID AND P.Category = 'Books');
(3) List the ProductID and Name of products whose names contain the substring 'pro' and are
priced below 1000. [4 points]
SELECT _______________________________________________________
FROM _________________________________________________________
WHERE _______________________________________________________;
Answer:
SELECT ProductID, Name
FROM Product
WHERE Name LIKE '%pro%' AND Price < 1000;
(4) Create a view called CustomerOrders that contains the CustomerID, CustomerName, and
OrderID for all orders placed by customers from 'New York'. [4 points]
Answer:
CREATE VIEW CustomerOrders (CustomerID, CustomerName, OrderID)
AS SELECT C.CustomerID, C.Name, O.OrderID
FROM Customer AS C, Order AS O
WHERE C.CustomerID = O.CustomerID AND C.City = 'New York';
(5) List the ID of products that have NOT been ordered by any customer from 'Los Angeles,'
order results by product ID in descending order. [4 points]
(SELECT
FROM _________________________________________________________)
_________________________________________________________________
(SELECT DISTINCT OI.ProductID
FROM OrderItem AS OI, __________________________________________
WHERE ________________________________________________________)
________________________________________________________________;
Answer:
(SELECT DISTINCT ProductID FROM Product)
EXCEPT
(SELECT DISTINCT OI.ProductID FROM OrderItem AS OI, Order AS O, Customer AS C
WHERE OI.OrderID = O.OrderID AND O.CustomerID = C.CustomerID AND C.City = 'Los
11
Angeles')
ORDER BY ProductID DESC;
12