0% found this document useful (0 votes)
93 views

Um6p Cs Introdb 2023 Midterm Exam Solutions

This document contains the solution to a closed-book midterm exam correction for an Introduction to Databases course. It includes solutions to conceptual modeling, logical modeling, functional dependencies and normalization, and SQL questions. The solutions demonstrate conceptual modeling with entities, attributes, keys and relationships; logical modeling with relational algebra queries; functional dependency violations and normalization decompositions; and SQL queries and views.

Uploaded by

ahmed lahlou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Um6p Cs Introdb 2023 Midterm Exam Solutions

This document contains the solution to a closed-book midterm exam correction for an Introduction to Databases course. It includes solutions to conceptual modeling, logical modeling, functional dependencies and normalization, and SQL questions. The solutions demonstrate conceptual modeling with entities, attributes, keys and relationships; logical modeling with relational algebra queries; functional dependency violations and normalization decompositions; and SQL queries and views.

Uploaded by

ahmed lahlou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Program CP Course Introduction to Databases

Academic Year 2022/2023 Teaching 64 H


Hours

Closed-Book Midterm Exam Correction

Part A: Conceptual Modeling (5 points)

Question 1 (3 points)

Solution:
• Entities = 0.75 pt (0.25 each)
• Attributes = 0.5 pt
• Primary keys = 0.75 pt (0.25 each)
• Relationship and cardinality = 1 pt (0.50 each with its cardinalities)

Course: Introduction to Databases Professor: Karima Echihabi 1/7


Item Shipped Event

Received

RetailCenter

Question 2 (2 points)

Solution:

Event (ScheduleNumber: int, Delivery Route: string, Type: string);


Item (ItemNumber: int, Weight: float, Dimension: string, InsuranceAmount: float, Destination:
string, FinalDeliveryDate: date, UniqueID:int); (UniqueID is a FK from RetailCenter)
Shipment (ScheduleNumber: int, ItemNumber:int); (ScheduleNumber is a FK from Event and
ItemNumber is a FK from Item)
RetailCenter (UniqueID: int, Address: string, Type: string)

4 relations with their PKs: 1pt (0.25 each)


Types: 0.25
3 Foreign Keys: 0.75 pt (0.25 each)

Part B: Logical Modeling (5 points)

Question 1 (2 points)

Solution: (0.5 pt each)

a. Find names of all courses offered by UM6P-CS


b. Error or Empty result set
c. List the names of schools and 3-credit courses they offer, including only the UM6P-
CS and EMINES schools .
d. Find the names of all courses/schools such that the schools offer courses that are
worth over 1000 dhs.

Course: Introduction to Databases Professor: Karima Echihabi 2/7


Question 2 (3 points)

Solution: (1 pt each)
a. ρ(E1,Enrollment)
ρ(E2,Enrollment)
ρ(E3 (1->stid, 2->cid 3->grade),E1 ⨝E1.grade < E2.grade E2))
πstid (E1 – πstid,cid,grade E3)
b. ρ(B, πcid (Courses) )
ρ(A, πstid,cid(Enrollment) )
ρ(Temp1, πstid(A) x B)
ρ(Temp2, Temp1 - A)
πstid ( A) – πstid (Temp2)
c. ρ(C1,Course_Offerings)
ρ(C2, Course_Offerings)
ρ(C3 (1->sid, 2->cid 3->fee),C1 ⨝C1.fee < C2.fee C2))
πcid (C1 – πsid,cid,fee C3)

Part C: Functional Dependencies and Normalization (5 points)

Question 1 (3 points)

Solution:

a. (0.5 pt)
B+ = B (B→B)
= BC (B→C)
= BCD (C→D)
= ABCD (C→A)
so the candidate key is B.
B is the ONLY candidate key, because nothing determines B

b. (1 pt)
R is not 3NF, because:
C→D causes a violation,
C→D is non-trivial ({D} ⊄ {C}).
C is not a superkey.
D is not part of any candidate key.
C→A causes a violation
Similar to above
B→C causes no violation
Since R is not 3NF, it is not BCNF either

Course: Introduction to Databases Professor: Karima Echihabi 3/7


c. (1.5 pt)
R is not in BCNF, so we will decompose it into a set of BCNF relations
C→D and C→A both cause violations of BCNF.
Take C→D: decompose R to R1= {A, B, C} , R2={C, D}.
Now check for violations in R1 and R2.
R1 still violates BCNF because of C→A.
Decompose R1 to R11 = {B, C} R12 = {C, A}.
Final decomposition: R2 = {C, D}, R11 = {B, C}, R12 = {C, A}.
No more violations.

Question 2 (2 points)

Solution:

1. (0.5 pt) Notice that we have single attribute on the RHS in all FDs, we need to look
for extraneous (redundant) attributes on the LHS and also look for FDs that are
redundant. We will apply the steps summarized in the document week3-notes.pdf
(section 3.3.4).
2. (1 pt) Find extraneous attributes on the LHS.

Consider ACD -> B to see if any of the three attributes on the LHS is extraneous. Start with
A. We need to compute ({ACD} – A)+ using the dependencies in F and check if the result
contains A; if it does, attribute A is extraneous.

Computing CD+, we get ACDEGB which contains A and therefore, A is extraneous. Also
the closure contains B, which tells us that CD -> B holds.

So the current set of FDs after step2 is :: {AB -> C, C -> A, BC -> D, D -> E, D -> G, BE ->
C, CG -> B, CG->D, CE -> A, CE -> G , CD->B}

3. (0.5 pt) Look for redundant FDs in set F.


• Is CE -> A redundant? Yes, since we have C -> A in F, we can get CE -> A
through augmentation.
• Is CG -> B redundant? Yes, since we have CG -> D, C -> A, and ACD -> B in F.

No more redundant FDs in F.

Minimal Cover: {AB -> C, C -> A, BC -> D, D -> E, D -> G, BE -> C, CG -> D, CE -> G,
CD -> B}

Note that the minimal cover is not unique, depending on the order in which the FDs are
processed. For example, if we process the FD: CD->B first in step3, then the resulting
minimal cover is: {AB -> C, C -> A, BC -> D, D -> E, D -> G, BE -> C, CG -> B, CE -> G}

Course: Introduction to Databases Professor: Karima Echihabi 4/7


Part D: SQL (5 points)

Question 1 (2 points)

Solution: (0.50 pt each)

a.

r1 r2 s1 s2
3 3 3 2
4 6 4 1

b.

r1 r2 s1 s2
1 7 Nul null
l
2 5 Nul Nul
l l
3 3 3 2
4 6 4 1

c.

r1 r2 s1 s2
3 3 3 2
4 6 4 1
null null 5 0
null null 6 3

d.

r1 r2 s1 s2
1 7 Nul null
l
2 5 Nul null
l
3 3 3 2

Course: Introduction to Databases Professor: Karima Echihabi 5/7


4 6 4 1
null null 5 0
null null 6 3

Question 2 (2 points)

Solution: (0.50 pt each)

a. List the names of the products that cost over 100 dhs and the names of the companies that
sell them

b. List the company id of all the companies that only sell products that cost over 200 dhs.

c. List the company id of all the companies that do not sell any products.

d. For each company, list the company id and the name of its most expensive product. If
multiple products are tied for highest price, return all of them.

Question 3 (1 point)

Solution: (0.50 pt each)

a. CREATE TABLE Product (


pname varchar (10),
price int,
cid int,
PRIMARY KEY (pname),
FOREIGN KEY (cid) REFERENCES Company (cid) ON DELETE NO ACTION
);

ON DELETE NO ACTION means that a parent cannot be deleted if it has children. Although
this is the default in most DBMS systems, it is better to explicitly state it to indicate to other
programmers the intended outcome so nobody mistakenly adds ON DELETE CASCADE.

b. The main idea here is to use the VIEW statement which enables logical data
independence by insulating changes in the database schema from application code.

CREATE TABLE Product_New (


pname varchar (10),

Course: Introduction to Databases Professor: Karima Echihabi 6/7


price int,
PRIMARY KEY (pname)
);

CREATE TABLE Offers (


product_name varchar (10),
cid int,
PRIMARY KEY (pname,cid)
FOREIGN KEY (cid) REFERENCES Company (cid) ON DELETE NO ACTION,
FOREIGN KEY (pname) REFERENCES Product_New (pname) ON DELETE
NO ACTION
);

CREATE VIEW Product AS


SELECT pname, price, cid
FROM Offers o, Product_New p
WHERE p.pname = o.product_name
;

Alternatively, the Product_New table could contain only pname and Offers contain price and
the View Product would be a simple select from the offers table.

Course: Introduction to Databases Professor: Karima Echihabi 7/7

You might also like