0% found this document useful (0 votes)
18 views11 pages

CS331 Final Exam Fall23

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)
18 views11 pages

CS331 Final Exam Fall23

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/ 11

Name: Last Name:

Subject:

Cohort: Classroom:

Grade/100 CS331 Final Exam Supervisors Signature

Database Management Systems


PROFESSOR NAME: SYRINE BEN MESKINA & EMNA
REJAIBI
Fall 2023
Date: 18/12/2023
Time: 11.30am - 1.30pm
Classroom visit: 12pm

INSTRUCTIONS

1. Books and notes are not allowed.


2. Cell phones are not allowed.
3. Calculators are not allowed.

Statement of Academic Integrity

Statement on Academic Integrity


I pledge to pursue all academic endeavors while at MedTech with honor and integrity. I understand the principles of
the MedTech Honor Code, and I promise to uphold and respect them. I pledge not to ask for or give information
pertaining to any examination before or after I have taken it, in such a way as to gain or give an advantage to one
student over another. I understand to be honor code violation: The giving and/or receiving of unauthorized aid on a
paper, problem, homework, exam, computer program, or other assignment submitted to meet course requirements;
plagiarism on an assigned paper, report, exam, or other submission; failure to report a known or suspected violation
of the Honor Code; any action designed to deceive a member of the faculty, staff, or a fellow student regarding the
principles mentioned in the Code; the submission of work prepared for another course; the use of texts, papers,
computer programs, or other class work prepared by commercial or noncommercial agents and submitted as my own
work; the falsification research results; or the altering of a previously graded exam or test with the objective of having
the original grade reconsidered.
Ps: All classrooms are equipped with video cameras.
Statement to be signed by the student:
I confirm that I have read and understood the statement above on academic integrity and certify that I did
not commit and did not attempt to commit academic fraud in this examination.
Signed: ______________________________________
Note: an examination copy or booklet without that signed statement will not be graded and will receive an exam
grade of zero.

Mediterranean Institute of Technology Final Exam Fall 2023 1/11


PART I – ENTITY-RELATIONSHIP DIAGRAM (ERD) & RELATIONAL MODEL (RM) (30 POINTS)
Question A) Considering the two following ERDs, what is the difference between “R” from the left-side ERD and “B” from the
right-side ERD? Does this impact or affect the logical as well as the physical structure of the database? [4pts]

…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………

Question B) Consider the ERDs (or ER models) 1 to 4 and the RMs (a) to (j) given below.

Mediterranean Institute of Technology Final Exam Fall 2023 2/11


B(b) (e) B(b) (h) B(b)
C(c) R(#b,c,#d,r), extra key: b,d C(c)
(a)
D(d,c) S(c,#d,s) D(c,d,s)
S(s, #c, #d, #c’) R(b,c,d,r), extra key: b,d
R(#b,#c,#d,r, #c’) (f) B(b)
C(c) (i) B(b)
B(b) D(c,d,s) C(c)
(b)
C(c) R(b,c,c’,d,r) D(c,d,s)
D(d) R(#b,#c,#c’,r) extra key: #b,#c,#c’
S(#d,#c, s) (g) B(b)
R(#b,#c,#d,r), extra key: b,#c,#d C(c) (j) B(b)
D(c,d,s) C(c)
(c)
B(b) R(b,c,d,r), extra key: b,d D(d)
C(c) S(#d,#c, s)
D(#c,d,s) R(#b,#c,#d,r)
R(#b,c,c’,d,r), extra key: b,d,c’
(d)
B(b)
C(c)
D(d)
S(#d,#c, s)
R(#b,#c,#d,r)

For each ERD, give a relational model that represents it correctly by adding a checkmark (X) in the corresponding field in the
table below. If an ERD is represented by none of the given RMs, then add a checkmark in the “none” column. [6pts]
Relational Models
ERDs
none (a) (b) (c) (d) (e) (f) (g) (h) (i) (j)
ER Model 1
ER Model 2
ER Model 3
ER Model 4

Question C) The graphical RM below is designed for a sale company. It tracks the customers, invoices, lines, products, and
vendors as well as some eventually existing connections between them. [10pts]

Mediterranean Institute of Technology Final Exam Fall 2023 3/11


Draw an appropriate ERD for the logical model given above. Your ERD should consider the following:
- Both vendors and customers have some common information such as their names, phones, area codes, …
- Products could be categorized into categories, in such a way a certain product could be found in different categories.
- vendors are supervised by some other vendors for a given period to ensure smooth sale activities.

C) Propose a textual RM to the ERD you provided in the previous question. [10pts]
Example of textual representation for a relation Rel
Rel ( attribute 1, attribute 2, …)
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………

Mediterranean Institute of Technology Final Exam Fall 2023 4/11


PART II – STRUCTURED QUERY LANGUAGE (SQL) & INTEGRITY CONSTRAINTS (30 POINTS)
Consider the database corresponding to the logical model you proposed in Part I. Additionally, suppose you have the following
data for the table Product.
P_code P_descript P_indate P_qoh P_min P_price P_discount V_code Where
P001 laptop 2023-01-15 50 10 899.99 0.05 V001 p_code: Product code, a unique identifier for each
P002 Smartphone 2023-02-20 80 15 699.99 0.1 V002 product.
P003 Wireless 2023-03-10 30 5 199.99 0.02 V003 p_description: Description of the product.
Headphones p_indate: Induction date, the date when the
P004 Digital 2023-04-05 40 8 499.99 0.07 V001 product was introduced.
Camera p_qoh: Quantity on hand, the current stock of the
P005 Smart TV 2023-05-18 20 4 1299.99 0.1 V002 product.
p_min: Minimum quantity, the minimum stock
P006
level that should be maintained.
P007
p_price: Price of the product.
P008
p_discount: Discount applicable to the product.
P009
v_code: Vendor code, indicating the vendor
associated with the product.
Question A) What will the following query be returning? (interpretation and result
set) [5pts]
SELECT P_CODE, P_PRICE
FROM PRODUCT
WHERE P_PRICE >= (SELECT AVG(P_PRICE)
FROM PRODUCT);

…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………….

Question B) Write a SQL query that displays the code and description of all products for which the discounted price is less than
the average price of all stored products (not strictly less than). [4pts]
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………….

Question C) Write a SQL query that displays the vendors’ codes who provide at least two different products. [4pts]
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………….

Question D) Display the name of the vendor providing the greatest number of different products. [4pts]
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………

………………………………………………………………………………………………………………………………………
Mediterranean Institute of Technology Final Exam Fall 2023 5/11
…………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………….

Question E) Display the vendors’ codes providing more than three different products. [4pts]
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………

Question F) Display all the customers and vendors stored in the database. [4pts]
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………

Question G) Write a SQL query that updates the database schema to take into consideration the deletion of products
in all related invoices. However, products update does not affect any of the related tables. [5pts]
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………

PART III – RELATIONAL ALEGBRA (20 POINTS)


Question A)
1- Translate the SQL query you proposed for Question C - Part II to a relational algebra expression. [4pts]
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………

Mediterranean Institute of Technology Final Exam Fall 2023 6/11


2- Write a relational algebra expression that calculates the most expensive product. [4pts]
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………

3- Write a relational algebra expression that identifies the invoices including all the products of the database. [4pts]
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………

Question B)
Consider the following relations and the relational algebra expressions below. Your task is to evaluate the expressions and to
draw the resulting schema corresponding to each (from (a) to (d)). [18pts]

Mediterranean Institute of Technology Final Exam Fall 2023 7/11


Mediterranean Institute of Technology Final Exam Fall 2023 8/11
PART IV – NOSQL & NON-RELATIONAL DATABASES (20 POINTS)
While working on a SQL database, assume you have a table named people having the following schema:
CREATE TABLE people (
id int NOT NULL
AUTO_INCREMENT,
user_id varchar(30),
age int,
status char(1),
PRIMARY KEY (id)
)
To migrate from a relational database to a schema-less one using MongoDB, assume you have a collection named people:
db.createCollection(“people”)
The latter contains documents of the following prototype:
{
_id: ObjectId("509a8fb2f3f4948bd2f983a0"),
user_id: "abc123",
age: 55,
status: "A"
}
Question A) Map the following statements from SQL to Mongo and vice versa. If the mapping is not applicable, write N/A. Each
row of the table has one statement to be mapped (Statements are completely independent from each others).

SQL Query Mongo Statement

…………………………………………………………………………………………………………………
INSERT INTO people(user_id, age, status)
…………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………
VALUES ("bcd001", 45, “A”)
…………………………………………………………………………………………………………………
………………………………………………………………………………………………………………..

………………………………………………………………………………………………………… db.people.updateMany(
…………………………………………………………………………………………………………
{ },
…………………………………………………………………………………………………………
………………………………………………………………………………………………………… { $set: { join_date: “mm-dd-yyyy” } }

………………………………………………………………………………………………………… )

“mm-dd-yyyy”: is the default value of type: varchar(10)


………………………………………………………………… db.people.update(
………………………………………………………………… { status: “A” },
………………………………………………………………… { $unset: { join_date: “” } },
………………………………………………………………… { multi: true }
………………………………………………………………… )
…………………………………………………………………
…………………………………………………………………

Mediterranean Institute of Technology Final Exam Fall 2023 9/11


………………………………………………………………………………………………………… db.people.update(
…………………………………………………………………………………………………………
{ status: “A” },
…………………………………………………………………………………………………………
………………………………………………………………………………………………………… { $unset: { join_date: “” } },
…………………………………………………………………………………………………………
{ multi: true }
……………………
)

………………………………………………………………………………………………………… db.people.find(
…………………………………………………………………………………………………………
{ },
…………………………………………………………………………………………………………
………………………………………………………………………………………………………… { user_id: 1, status: 1 }
…………………………………………………………………………………………………………
)
……………………
SELECT * …………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………
FROM people
…………………………………………………………………………………………………………………
WHERE status = "A" …………………………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………………………………………………
SELECT user_id, status …………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………
FROM people
…………………………………………………………………………………………………………………
WHERE age > 25 …………………………………………………………………………………………………………………
……………………………………
SELECT * …………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………
FROM people
…………………………………………………………………………………………………………………
WHERE age > 25 …………………………………………………………………………………………………………………
………………………………………………………………………………………………
AND age <= 50

…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
………………………………………………………………………………………………………… db.people.find( { status: "A" } ).sort( { user_id: -1 } )

…………………………………………………………………………………………………………
…………………………………………………………………………………………………………

UPDATE people …………………………………………………………………………………………………………………


…………………………………………………………………………………………………………………
SET status = "C"
…………………………………………………………………………………………………………………
WHERE age > 25
………………………………………………………………………

…………………………………………………………………………………………………………
db.people.find(
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
{ age: { $exist: false} } )
………………………………………………………………………………

Mediterranean Institute of Technology Final Exam Fall 2023 10/11


Question B) For each question, tick the correct answer:
1. SQL databases are: 6. What is the main benefit of using NoSQL databases?

A. Horizontally scalable A. Strict data modeling


B. Vertically scalable B. Limited scalability
C. Either horizontally or vertically scalable C. Easy schema evolution
D. They don't scale. D. Limited data storage capacity
2. Which of the following is a reason to use a SQL database? 7. Which of the following is true?

A. It can easily store unstructured data Statement 1: All NoSQL databases support ACID transactions.
B. It's ACID-compliant Statement 2: NoSQL databases come in a variety of types based on
C. It can enable development in the cloud their data model.
D. All of the above
A. Both statement 1 and statement 2 are true
B. None of the statements is true
C. Only statement 1 is true
D. Only statement 2 is true
3. Which of the following is a characteristic of a NoSQL 8. What is the challenge of using NoSQL databases?
database?
A. Limited scalability
A. Uses tables for storage B. Limited support for unstructured data
B. Needs a schema C. Limited query capabilities
C. Requires JOINs D. None of the above
D. Uses JSON
4. Which of the following is a primary classification for 9. SQL command types include data manipulation language
NoSQL architectures? (DML) and data definition language (DDL).

A. Document databases A. True


B. Graph databases B. False
C. Key-value databases
D. All of the above

5. SQL command types include data manipulation language 10. Which one of the following represents “column” in
(DML) and data definition language (DDL). MongoDB __________.

A. True A. Database
B. False B. Field
C. Collection
D. Document

END
Good Luck

Mediterranean Institute of Technology Final Exam Fall 2023 11/11

You might also like