100% found this document useful (1 vote)
113 views

FIT9132 Tutorial 4 Sample Solution

Uploaded by

Le Duy Tho
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
113 views

FIT9132 Tutorial 4 Sample Solution

Uploaded by

Le Duy Tho
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

FIT9132 Introduction to Databases

Week 4 Tutorial Suggested Solution

FIT Database Teaching Team

FIT9132 2020 S1

FIT9132 Introduction to Databases

Author: FIT Database Teaching Team

License: Copyright © Monash University, unless otherwise stated. All Rights Reserved.

COPYRIGHT WARNING
Warning

This material is protected by copyright. For use within Monash University only. NOT FOR RESALE.

Do not remove this notice.

Page 1 of 5
4.1 The Relational Model
4.1.1 Lead Tutor Explanation Part A

CUSTOMER (CUST_ID, CUST_NAME, CUST_ADDRESS)


ORDER (ORDER_ID, ORDER_DATE, CUST_ID)

Assume a single customer may have any number of orders.

Why would we not just store all the customer and order information in one relation so as to avoid a
join operation?

Doing so would result in substantial data redundancy and would lead to insert, update and delete
anomalies.

1. Relation: A named set of attributes, consisting of a heading and a body. The heading is the
schema, the body is the set of tuples (the state at a point in time)

2. Attribute: A characteristic of an object/entity that we wish to record eg. customer_balance

3. Domain: A set of atomic (indivisible) values from which an attribute's values are drawn.
Consists of a name, data type and data format eg. gender domain: one character string with
allowed values of M and F

4. Tuple: A set of related attributes describing a particular instance of the relation /entity – in file
terminology called a row

5. Degree and cardinality of a relation:

Degree: number of attributes in a relation;

Cardinality: number of tuples

6. Primary key and foreign key:

Primary key: an attribute or minimal set of attributes which uniquely identify each tuple;

Foreign key: an attribute(s) in a relation that exists in the same or another relation as
Primary Key;

4.1.2 Choosing the Primary key

1. In any relation, tuples must be unique. However, in many cases, the set of all the attributes in
a relation is not considered a candidate key. Why not?

Although all of the attributes in a relation is a superkey, the candidate key is selected on the
basis of a minimum superkey. For a given relation there is usually a smaller set of
attributes that provide a superkey so there is no need to consider the full set of attributes as
a starting point.

On the other hand, suppose we do have a relation where the set of all attributes is a
candidate key. In this case, show that this set must, therefore, be the only candidate key
and hence the primary key.

Page 2 of 5
If the set of all attributes is a candidate key (ie. a minimum superkey), there can be no other
superkey and hence it must be the only candidate key and the primary key.

2. Underlined attribute(s) is the primary key

ORDER (ORDER_ID, ORDER_DATE, CUST_ID)


ORDERLINE (ORDER_ID, PROD_NO, OL_QTYORDERED, OL_LINEPRICE)
PRODUCT (PROD_NO, PROD_DESC, PROD_UNITPRICE)

The foreign keys are order_id and prod_no in ORDERLINE. order_id in ORDERLINE refers
to order_id in ORDER and prod_no in ORDERLINE refers to prod_no in PRODUCT.

3. Consider a relation that depicts a dental surgery appointment system.


APPOINTMENT (dentist_id, dentist_name, patient_id, patient_name, appointment_datetime,
surgeryroom_no)

Candidate keys:
(dentist_id, appointment_datetime)
(patient_id, appointment_datetime)
(surgeryroom_no, appointment_datetime)
Primary key:
One of the candidate key

4.2 Relational Algebra


4.2.1 Relational Algebra Exercise

HOTEL (HOTEL_NO, HOTEL_NAME, HOTEL_CITY)


ROOM (ROOM_NO, HOTEL_NO, ROOM_TYPE, ROOM_PRICE)
BOOKING (HOTEL_NO, GUEST_NO, BDATE_FROM, BDATE_TO, ROOM_NO)
GUEST (GUEST_NO, GUEST_NAME, GUEST_ADDRESS)

1. List the names and cities of all hotels

Answer1 = π hotel_name, hotel_city HOTEL

2. List all single rooms with a price below $50

Answer2 = σ room_type='single' and room_price < 50 ROOM

3. List the names of all hotels in Melbourne

Answer3 = π hotel_name(σ hotel_city='Melbourne' HOTEL))

Page 3 of 5
4. List all names of hotels which have presidential suite room

PSuiteNo = π hotel_no (σ room_type = 'presidential suite' ROOM)

Answer4 = π hotel_name (PSuiteNo ⨝ (π hotel_no, hotel_name HOTEL))


or
Answer4 = π hotel_name ((π hotel_no (σ room_type = 'presidential suite' ROOM)) ⨝
(π hotel_no, hotel_name HOTEL))

5. List the price and type of all rooms at the Grosvenor Hotel

GrosvenorNo = π hotel_no (σ hotel_name = 'Grosvenor' HOTEL)

Answer5 = π room_price, room_type (GrosvenorNo ⨝ (π hotel_no, room_price, room_type ROOM))


or
Answer5 = π room_price, room_type ((π hotel_no (σ hotel_name = 'Grosvenor' HOTEL)) ⨝
(π hotel_no, room_price, room_type ROOM))

6. List all names and addresses of guests currently staying in deluxe room of any hotel
(assume that if the guest has a tuple in the BOOKING relation, then they are currently
staying in the hotel)

DeluxeNo = π hotel_no, room_no (σ room_type = 'deluxe' ROOM)

GuestDeluxeNo = π guest_no (DeluxeNo ⨝ (π guest_no, hotel_no, room_no BOOKING))


Answer6 = π guest_name, guest_address (GuestDeluxeNo ⨝ GUEST)
or
Answer6 = π guest_name, guest_address ((π guest_no ((π hotel_no, room_no (σ room_type = 'deluxe' ROOM)) ⨝
(π guest_no, hotel_no, room_no BOOKING))) ⨝ GUEST)

7. List all names and addresses of guests currently staying at the Grosvenor Hotel (assume
that if the guest has a tuple in the BOOKING relation, then they are currently staying in the
hotel)

GrosvenorNo = π hotel_no (σ hotel_name = 'Grosvenor' HOTEL)

GrosvenorBookings = π guest_no (GrosvenorNo ⨝ (π guest_no, hotel_no BOOKING))


Answer7 = π guestl_name, guest_address (GrosvenorBookings ⨝ GUEST)
or
Answer7 = π guest_name, guess_address ((π guest_no ((π hotel_no (σ hotel_name = 'Grosvenor' HOTEL)) ⨝
(π guest_no, hotel_no BOOKING))) ⨝ GUEST)

Page 4 of 5
4.2.2 Relational Algebra Operators

NATURAL JOIN of ORDER and CUSTOMER

– CUSTOMER table:

Cust_ID Name
1 Green
2 Blue

ORDER table:

Ord_ID Date Cust_ID


1 23-Feb-2014 1
2 26-Feb-2014 1
3 26-Feb-2014 2

Step 1: Cartesian Product

Name C.Cust_ID O.Cust_ID Ord_ID Date


Green 1 1 1 23-Feb-2014
Blue 2 1 1 23-Feb-2014
Green 1 1 2 26-Feb-2014
Blue 2 1 2 26-Feb-2014
Green 1 2 3 26-Feb-2014
Blue 2 2 3 26-Feb-2014

Step 2: Select C.Cust_ID = O.Cust_ID (note this is an Equi Join)

Name C.Cust_ID O.Cust_ID Ord_ID Date


Green 1 1 1 23-Feb-2014
Green 1 1 2 26-Feb-2014
Blue 2 2 3 26-Feb-2014

Step 3: Project away one of the Cust_ID columns

Final result (Natural Join):

Cust_ID Ord_ID Name Date


1 1 Green 23-Feb-2014
1 2 Green 26-Feb-2014
2 3 Blue 26-Feb-2014

Page 5 of 5

You might also like