You are on page 1of 3

Teaching and Learning Activity: Suggested Solutions

Module: Database Programming 2 (HDBP2161)


Week number (Date): 4 (27 August 2020)
Unit covered: Unit 4

Use the diagrams below to answer the following relational algebra questions.

DRIVER
ID F_NAME SURNAME CELL ADDRESS
100 Charles Muranda 0123456789 21 Cape Town City
101 Joe Doe 0987654321 35 Johannesburg City
103 John Smith 0876543219 42 Durban City
104 Sarah Simpsons 0765432123 55 Pretoria North

TRIPS
ID DRIVER_ID DEPARTURE_CITY_ID ARRIVAL_CITY_ID GOODS_ID DEPARTURE_DATE STATUS

5 100 15 25 101 2020-04-20 3


6 104 01 20 110 2020-04-30 2
7 101 25 10 125 2020-07-31 2
8 103 01 20 101 2020-08-05 1

CITY STATUS
ID CITY_NAME COUNTRY_ID ID STATUS
01 Kinshasa 103 1 In transit
05 Durban 100 2 Delivered successfully
10 Windhoek 102 3 Delivered with challenges in transit
15 Lubumbashi 103
20 Johannesburg 100
25 Gaborone 105

GOODS
ID NAME
101 Minerals
105 Petroleum
108 Medical supplies
110 Cars
115 Chemicals
120 Beef

1 HDBP200-1-Jul-Dec2020-T&L-Memo-CMu-W4-18082020
a. Determine the appropriate relational algebra expressions that would give the following results from
the above tables:

1.
ID F_NAME SURNAME CELL ADDRESS
100 Charles Muranda 0123456789 21 Cape Town City
101 Joe Doe 0987654321 35 Johannesburg City
103 John Smith 0876543219 42 Durban City

Solution
𝜎𝐷𝑅𝐼𝑉𝐸𝑅.𝐼𝐷<104 (𝐷𝑅𝐼𝑉𝐸𝑅)

NB: use σ when fetching all the columns of the table

2.
F_NAME SURNAME
Charles Muranda
Joe Doe
John Smith
Sarah Simpsons

Solution
𝜋𝐹𝑁𝐴𝑀𝐸 ,𝑆𝑈𝑅𝑁𝐴𝑀𝐸 (𝜎𝐷𝑅𝐼𝑉𝐸𝑅.𝐼𝐷<104 (𝐷𝑅𝐼𝑉𝐸𝑅))

NB use π when you do not want to select all the columns.

3.
F_NAME DEPARTURE_DATE GOODS.NAME
Charles 2020-04-20 Minerals

Solution

Here the result is grabbed from 3 tables (DRIVERS, TRIPS, GOODS).

• Starting with the TRIPS table, we need to join it with the DRIVERS table (the join condition is (DRIVERS.ID
= TRIPS.DRIVER_ID) and we only want where DRIVER_ID is 100:

𝜎𝐷𝑅𝐼𝑉𝐸𝑅𝑆.𝐼𝐷=𝑇𝑅𝐼𝑃𝑆.𝐷𝑅𝐼𝑉𝐸𝑅_𝐼𝐷 , 𝑇𝑅𝐼𝑃𝑆.𝐷𝑅𝐼𝑉𝐸𝑅_𝐼𝐷=100 (𝐷𝑅𝐼𝑉𝐸𝑅𝑆 × 𝑇𝑅𝐼𝑃𝑆)

• Let us then join the result with the goods table

𝜎𝐺𝑂𝑂𝐷𝑆.𝐼𝐷=𝑇𝑅𝐼𝑃𝑆.𝐺𝑂𝑂𝐷𝑆𝐼𝐷 (𝐺𝑂𝑂𝐷𝑆 × (𝜎𝐷𝑅𝐼𝑉𝐸𝑅𝑆.𝐼𝐷=𝑇𝑅𝐼𝑃𝑆.𝐷𝑅𝐼𝑉𝐸𝑅𝐼𝐷 , 𝑇𝑅𝐼𝑃𝑆.𝐷𝑅𝐼𝑉𝐸𝑅𝐼𝐷 =100 (𝐷𝑅𝐼𝑉𝐸𝑅𝑆 × 𝑇𝑅𝐼𝑃𝑆)))

2 HDBP200-1-Jul-Dec2020-T&L-Memo-CMu-W4-18082020
• Now that we are done with joining all tables, let us select the required columns:

𝜋𝐹_𝑁𝐴𝑀𝐸, 𝐷𝐸𝑃𝐴𝑅𝑇𝑈𝑅𝐸_𝐷𝐴𝑇𝐸, 𝐺𝑂𝑂𝐷𝑆.𝑁𝐴𝑀𝐸 (𝜎𝐺𝑂𝑂𝐷𝑆.𝐼𝐷=𝑇𝑅𝐼𝑃𝑆.𝐺𝑂𝑂𝐷𝑆𝐼𝐷 (𝐺𝑂𝑂𝐷𝑆

× (𝜎𝐷𝑅𝐼𝑉𝐸𝑅𝑆.𝐼𝐷=𝑇𝑅𝐼𝑃𝑆.𝐷𝑅𝐼𝑉𝐸𝑅𝐼𝐷, 𝑇𝑅𝐼𝑃𝑆.𝐷𝑅𝐼𝑉𝐸𝑅𝐼𝐷 =100 (𝐷𝑅𝐼𝑉𝐸𝑅𝑆 × 𝑇𝑅𝐼𝑃𝑆))))

b. What is the outcome of the following relational algebra equations:

1. 𝜋𝐴𝐷𝐷𝑅𝐸𝑆𝑆 (𝜎(𝐷𝑅𝐼𝑉𝐸𝑅𝑆)) or just 𝜎𝐴𝐷𝐷𝑅𝐸𝑆𝑆 (𝐷𝑅𝐼𝑉𝐸𝑅𝑆)

Solution
ADDRESS
21 Cape Town City
35 Johannesburg City
42 Durban City
55 Pretoria North

2. 𝜋𝑆𝑈𝑅𝑁𝐴𝑀𝐸, 𝐺𝑂𝑂𝐷𝑆.𝑁𝐴𝑀𝐸 (𝜎𝐺𝑂𝑂𝐷𝑆.𝐼𝐷=𝑇𝑅𝐼𝑃𝑆.𝐺𝑂𝑂𝐷𝑆_𝐼𝐷 (𝐺𝑂𝑂𝐷𝑆 ×

(𝜎𝐷𝑅𝐼𝑉𝐸𝑅𝑆.𝐼𝐷=𝑇𝑅𝐼𝑃𝑆.𝐷𝑅𝐼𝑉𝐸𝑅_𝐼𝐷 (𝐷𝑅𝐼𝑉𝐸𝑅𝑆 × 𝑇𝑅𝐼𝑃𝑆))))

SOLUTION
SURNAME GOODS.NAME
Muranda Minerals
Simpsons Cars
Smith Minerals

3 HDBP200-1-Jul-Dec2020-T&L-Memo-CMu-W4-18082020

You might also like