0% found this document useful (0 votes)
5 views4 pages

Relational Expression exercises

Uploaded by

Symon Saroar
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)
5 views4 pages

Relational Expression exercises

Uploaded by

Symon Saroar
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/ 4

Exercise

A B
0 1
 R(A, B):
2 3 1. GammaAvg(A) (R) = {1}
0 1 2. GammaA,B (R) = {(0,1), (2,3), (2,4), (null,4)}
2 4 3. Count(*)=6 //the toal # of tuples in R
null 4 4. Count (A) =4 // the total # of non-null values in A
null 4
B C 5. GammaA (R) = {0, 2, null}
0 1  S(B , C):
2 4
1. GammaB (S) = {(0), (2), (3)}
2 5
2. GammaC, Avg(B) (S) ={(1, 0), (2,0), (4, 2.67), (5,2) }
3 4
0 2
3 4

 Database Schema:
Sailors(sid, sname)
Boats(bid, color)
Reserves(sid, bid, date)

• Underlined attributes are key attributes


• Assuming all operations take their SET
semantics.

1
Find names of sailors who’ve reserved boat
#103
 Solution 1: πsname((σbid=103(Reserves) ⋈ Sailors)
 Problem? What if two sailors have the same
name? We want to print out the name of a
qualified sailor exactly once, but SET projection
will eliminate ‘duplicate’ sailor names.
 Solution 2: πsid, sname(σbid=103(Reserves ⋈ Sailors))

 Solution 3: πsid, sname((σbid=103(Reserves) ⋈ Sailors)

Find sids and names of sailors who’ve ever


reserved a red boat
 Information about boat color only available in
Boats; so we need an extra join:
πsid, sname((σcolor=‘red’(Boats) ⋈ Reserves ⋈
Sailors)
 A likely more efficient solution:

πsid, sname(πsid((σcolor=‘red’(Boats) ⋈ Reserves) ⋈


Sailors)
A query optimizer can find this, given the first solution!

2
Find sailors who’ve ever reserved a red or a
green boat
 Can identify all red or green boats, then find sailors
wh’ve reserved one of these boats:
ρ(Tempboats, (σcolor=‘red’ or color=‘green’(Boats))
πsid, sname(πsid(Tempboats ⋈ Reserves) ⋈ Sailors)
 Can also define Tempboats using union

Tempboats= σcolor=‘red’(Boats) U
σcolor=‘green’(Boats)
 What happens if union is replaced by intersection in
this query? (Tempboat will be empty)

Find sailors who’ve ever reserved a red boat


and a green boat
 Previous approach won’t work! Must identify
sailors who’ve reserved red boats, sailors
who’ve reserved green boats, then find the
intersection (note that sid is a key for Sailors):
ρ(Tempred, πsid((σcolor=‘red’ (Boats)) ⋈ Reserves))
ρ(Tempgreen, πsid((σcolor=‘green’ (Boats)) ⋈ Reserves))
πsid,sname((Tempred ∩ Tempgreen) ⋈ Sailors)

3
Summary
 The relational model has rigorously defined
query languages that are simple and powerful.
 Relational algebra is more operational; useful
as internal representation for query evaluation
plans.
 Several ways of expressing a given query; a
query optimizer should choose the most(?)
efficient version.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy