0% found this document useful (0 votes)
57 views41 pages

5 - Relational Algebra - Post2

The document provides information about an upcoming midterm exam for CPSC 304 on February 26th at 6PM in HEBB 100 and notes that project repositories have been provisioned for student use and can be accessed on GitHub. It also lists the remaining project milestones and reminds students to complete tutorials ahead of assigned deadlines.

Uploaded by

xx1203120688
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)
57 views41 pages

5 - Relational Algebra - Post2

The document provides information about an upcoming midterm exam for CPSC 304 on February 26th at 6PM in HEBB 100 and notes that project repositories have been provisioned for student use and can be accessed on GitHub. It also lists the remaining project milestones and reminds students to complete tutorials ahead of assigned deadlines.

Uploaded by

xx1203120688
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

CPSC 304 – Administrative notes

Feb 26, 2024


February 26: Midterm 1 @ 6PM at HEBB 100
More information on Piazza.
Your repositories have now been provisioned and are
available for your use. It does not replace submitting on
Canvas.
You can find your repository link by logging in at
[Link] .
Your Oracle accounts are ready (more on that next slide)
Mid Course Feedback Survey (section 201):
[Link]
RA in class exercise 2 (from today) will be due on
Wednesday (a day late) due to the midterm
Let’s talk about the rest of the
project deliverables for a minute
PHP, JavaScript & JDBC tutorial are not in
section yet, but DO NOT WAIT to do the
tutorial that you are planning on using for
your project.
There are four remaining milestones:
Milestone 3: Meet with your TA MILESTONE 3
REQUIRES YOU TO TURN IN THINGS AHEAD
OF TIME
Milestone 4: Implementation. THIS IS WHEN
YOUR CODE IS DUE.
Milestone 5: Demo
Milestone 6: Individual & peer evaluations
51
Now where were we…
We’d just covered the basics of Relational
Algebra
Selection (σ): Selects a subset of rows from relation.
Projection (π): Deletes unwanted columns from relation.
Cross-product (x): Allows us to combine two relations.
Set-difference (-): Tuples in relation 1, but not in relation 2.
Union (È): Tuples in relation 1 and in relation 2.
Rename (ρ): Assigns a (another) name to a relation
Additional, inessential but useful operations:
Intersection (Ç), join (⋈), assignment(¬)

52
Equi-Join & Natural Join
Equi-Join: A special case of condition join R ⋈cS = σc(R×S),
where c contains only equalities. Only the first of the repeated
columns is retained.
Natural Join: Equijoin on all common attributes
Result schema: similar to cross-product, but has only one copy of each
common attribute
No need to show the condition
If the two attributes have no common attributes, this would be the same
as cross product.
This is what we saw in BCNF & 3NF

53
Equi and Natural Join Examples
MovieStar StarsIn
StarID Name Gender MovieID StarID Character
1 Harrison Ford Male 1 1 Han Solo
2 Ingrid Female 4 1 Indiana Jones
Bergman 2 2 Ilsa Lund
3 Judy Garland Female 3 3 Dorothy Gale

MovieStar ⋈ StarsIn
StarID Name Gender MovieID Character
1 Harrison Ford Male 1 Han Solo
1 Harrison Ford Male 4 Indiana Jones
3 Judy Garland Female 3 Dorothy Gale
2 Ingrid Bergman Female 2 Ilsa Lund 54
Movie(MovieID, Title, Year)
StarsIn(MovieID, StarID, Character)
Join Example MovieStar(StarID, Name, Gender)

Find the names of all Movie Stars who


were in any Movie

Name
Harrison Ford
Ingrid Bergman
Judy Garland

55
Movie(MovieID, Title, Year)
StarsIn(MovieID, StarID, Character)
Join Example MovieStar(StarID, Name, Gender)

Find the names of all Movie Stars who


were in any Movie

pname(MovieStar ⋈ StarsIn)
Name
Harrison Ford
Ingrid Bergman
Judy Garland

56
Assignment Operation
Notation: t ¬ E
assigns the result of expression E to a temporary
relation t.
Used to break complex queries to small steps.
Assignment is always made to a temporary
relation variable.
Example: Write r Ç s in terms of È and/or –
temp1 ¬ r - s
result ¬ r – temp1
r s
57
Find names of actors who have been in “Indiana Jones”
(note: examples use "Indiana Jones and Raiders of the Lost Ark", but no one
wants to write all that out)

(σTitle = “Indiana Jones” Movie)


MovieID Title Year
4 Indiana Jones and the 1981
Raiders of the Lost Ark

((σTitle = “Indiana Jones” Movie) ⋈ StarsIn)


MovieID Title Year StarID Character
4 Indiana Jones and the Raiders 1981 1 Indiana
of the Lost Ark Jones

(π Name((σTitle = “Indiana Jones” Movie) ⋈ StarsIn ⋈ MovieStar))

Name
Harrison Ford 60
Find the name of actors who have been in
“Indiana Jones” and “Star Wars”

Indy ß πstarID((σ Title = “Indiana Jones” Movie) ⋈ StarsIn)

StarWars ß πstarID((σ Title = “Star Wars” Movie) ⋈ StarsIn)

CoolPeopleßIndy ∩ StarWars

πname(CoolPeople ⋈ MovieStar)

(note: examples use "Indiana Jones and Raiders of the


Lost Ark", but no one wants to write all that out) 61
Find the name of actors who have been in
“Indiana Jones” and “Star Wars”

Would this work?

Indy ß πname((σ Title = “Indiana Jones” Movie) ⋈ StarsIn) ⋈ MovieStar

StarWars ß πname((σ Title = “Star Wars” Movie) ⋈ StarsIn)⋈ MovieStar

CoolPeopleßIndy ∩ StarWars

(note: examples use "Indiana Jones and Raiders of the


Lost Ark", but no one wants to write all that out) 65
Find the name of actors who have been in
“Indiana Jones” and “Star Wars”
MovieStar(StarID, Name, Gender) StarsIn(MovieID, StarID, Character)

StarID Name Gender MovieID StarID Character


1 Harrison Ford Male 1 4 Han Solo
2 Ingrid Female 4 1 Indiana Jones
Bergman
2 2 Ilsa Lund
3 Judy Garland Female 3 3 Dorothy Gale
4 Harrison Ford Male
Movie(MovieID, Title, Year)
MovieID Title Year
1 Star Wars 1977
2 Casablanca 1942
3 The Wizard of Oz 1939
4 Indiana Jones and the 1981
Raiders of the Lost Ark 66
Exercise
Find the names of actors who have been in
a movie with the same title as the actor’s
name

67
Clicker Exercise
Find the names of actors who have been in a
movie with the same title as the actor’s name
Which of the following does not do that correctly:
A. πName((Movie⋈ StarsIn) ⋈ title = name ^ StarID =

[Link] MovieStar)
B. πName(MovieStar⋈ Name = title ^ [Link] =
StarID (StarsIn⋈ Movie))
C. πName((StarsIn⋈ (π StarID,NameMovieStar))
⋈MovieID = [Link] ^ title = name Movie)
D. All are correct
All are correct (D)
E. None are correct 68
Clicker Exercise Explained Option A
πName((Movie⋈ StarsIn) ⋈ title = name ^ StarID = [Link]
MovieStar)

Movie ⋈ StarsIn
MovieID Title Year StarID Character
1 Star Wars 1977 1 Han Solo
2 Casablanca 1942 2 Ilsa Lund
3 The Wizard of Oz 1939 3 Dorothy
Gale
4 Indiana Jones and the 1981 1 Indiana
Raiders of the Lost Ark Jones

69
Clicker Exercise Explained Option A
πName((Movie⋈ StarsIn) ⋈ title = name ^ StarID = [Link]
MovieStar)

Why do we need starID = [Link]?

What does title = name do?

Let’s process this like a cross product and then


remove tuples that don’t fit our condition.

70
(Movie⋈ StarsIn) x MovieStar
MovieID Title Year 4 Character 6 Name Gender

1 Star Wars 1977 1 Han Solo 1 Harrison Ford Male


2 Casablanca 1942 2 Ilsa Lund 1 Harrison Ford Male
3 The Wizard of Oz 1939 3 Dorothy Gale 1 Harrison Ford Male
4 Indiana Jones and the 1981 1 Indiana Jones 1 Harrison Ford Male
Raiders of the Lost Ark
1 Star Wars 1977 1 Han Solo 2 Ingrid Bergman Female
2 Casablanca 1942 2 Ilsa Lund 2 Ingrid Bergman Female
3 The Wizard of Oz 1939 3 Dorothy Gale 2 Ingrid Bergman Female
4 Indiana Jones and the 1981 1 Indiana Jones 2 Ingrid Bergman Female
Raiders of the Lost Ark
1 Star Wars 1977 1 Han Solo 3 Judy Garland Female

2 Casablanca 1942 2 Ilsa Lund 3 Judy Garland Female

3 The Wizard of Oz 1939 3 Dorothy Gale 3 Judy Garland Female

4 Indiana Jones and the 1981 1 Indiana Jones 3 Judy Garland Female
Raiders of the Lost Ark 71
Remove tuples where starIDs
(columns 4 and 6) do not match
MovieID Title Year 4 Character 6 Name Gender

1 Star Wars 1977 1 Han Solo 1 Harrison Ford Male


2 Casablanca 1942 2 Ilsa Lund 1 Harrison Ford Male
3 The Wizard of Oz 1939 3 Dorothy Gale 1 Harrison Ford Male
4 Indiana Jones and the 1981 1 Indiana Jones 1 Harrison Ford Male
Raiders of the Lost Ark
1 Star Wars 1977 1 Han Solo 2 Ingrid Bergman Female
2 Casablanca 1942 2 Ilsa Lund 2 Ingrid Bergman Female
3 The Wizard of Oz 1939 3 Dorothy Gale 2 Ingrid Bergman Female
4 Indiana Jones and the 1981 1 Indiana Jones 2 Ingrid Bergman Female
Raiders of the Lost Ark
1 Star Wars 1977 1 Han Solo 3 Judy Garland Female

2 Casablanca 1942 2 Ilsa Lund 3 Judy Garland Female

3 The Wizard of Oz 1939 3 Dorothy Gale 3 Judy Garland Female

4 Indiana Jones and the 1981 1 Indiana Jones 3 Judy Garland Female
Raiders of the Lost Ark 72
Remove tuples where starIDs
(columns 4 and 6) do not match
MovieID Title Year 4 Character 6 Name Gender

1 Star Wars 1977 1 Han Solo 1 Harrison Ford Male


4 Indiana Jones and the 1981 1 Indiana Jones 1 Harrison Ford Male
Raiders of the Lost Ark
2 Casablanca 1942 2 Ilsa Lund 2 Ingrid Bergman Female
3 The Wizard of Oz 1939 3 Dorothy Gale 3 Judy Garland Female

Only keep tuples where title = name


MovieID Title Year 4 Character 6 Name Gender

Projection to only keep the Name column.


Name

Note: Based on this relational instance, there were no results but


this does not mean the relational algebra expression is incorrect.
73
Clicker Exercise Explained Option B
πName(MovieStar ⋈ Name = title ^ [Link] = StarID (StarsIn
⋈ Movie))

The same join as option A but the order of the


joins is different.

74
Clicker Exercise Explained Option C
πName((StarsIn⋈ (π StarID,NameMovieStar)) ⋈MovieID =
[Link] ^ title = name Movie)

π StarID,NameMovieStar
StarID Name
1 Harrison Ford
2 Ingrid Bergman
3 Judy Garland

StarsIn⋈ (π StarID,NameMovieStar)
MovieID StarID Character Name
1 1 Han Solo Harrison Ford
4 1 Indiana Jones Harrison Ford
2 2 Ilsa Lund Ingrid Bergman
3 3 Dorothy Gale Judy Garland 75
Clicker Exercise Explained Option C
πName((StarsIn⋈ (π StarID,NameMovieStar)) ⋈MovieID =
[Link] ^ title = name Movie)

StarsIn ⋈ (π StarID,NameMovieStar)
MovieID StarID Character Name
1 1 Han Solo Harrison Ford
4 1 Indiana Jones Harrison Ford
2 2 Ilsa Lund Ingrid Bergman
3 3 Dorothy Gale Judy Garland

Same as before. Let’s do the cross product and


eliminate tuples that do not meet our listed
condition.
76
1 StarID Character Name 5 Title Year

1 1 Han Solo Harrison Ford 1 Star Wars 1977

4 1 Indiana Jones Harrison Ford 1 Star Wars 1977

2 2 Ilsa Lund Ingrid 1 Star Wars 1977


Bergman
3 3 Dorothy Gale Judy Garland 1 Star Wars 1977

1 1 Han Solo Harrison Ford 2 Casablanca 1942

4 1 Indiana Jones Harrison Ford 2 Casablanca 1942

2 2 Ilsa Lund Ingrid 2 Casablanca 1942


Bergman
3 3 Dorothy Gale Judy Garland 2 Casablanca 1942

1 1 Han Solo Harrison Ford 3 The Wizard of Oz 1939

4 1 Indiana Jones Harrison Ford 3 The Wizard of Oz 1939

2 2 Ilsa Lund Ingrid 3 The Wizard of Oz 1939


Bergman
3 3 Dorothy Gale Judy Garland 3 The Wizard of Oz 1939

1 1 Han Solo Harrison Ford 4 Indiana Jones and the 1981


Raiders of the Lost Ark
4 1 Indiana Jones Harrison Ford 4 Indiana Jones and the 1981
Raiders of the Lost Ark
2 2 Ilsa Lund Ingrid 4 Indiana Jones and the 1981
Bergman Raiders of the Lost Ark
3 3 Dorothy Gale Judy Garland 4 Indiana Jones and the 1981
Raiders of the Lost Ark 77
1 StarID Character Name 5 Title Year

1 1 Han Solo Harrison Ford 1 Star Wars 1977

4 1 Indiana Jones Harrison Ford 1 Star Wars 1977

2 2 Ilsa Lund Ingrid 1 Star Wars 1977


Bergman
3 3 Dorothy Gale Judy Garland 1 Star Wars 1977

1 1 Han Solo Harrison Ford 2 Casablanca 1942

4 1 Indiana Jones Harrison Ford 2 Casablanca 1942

2 2 Ilsa Lund Ingrid 2 Casablanca 1942


Bergman
3 3 Dorothy Gale Judy Garland 2 Casablanca 1942

1 1 Han Solo Harrison Ford 3 The Wizard of Oz 1939

4 1 Indiana Jones Harrison Ford 3 The Wizard of Oz 1939

2 2 Ilsa Lund Ingrid 3 The Wizard of Oz 1939


Bergman
3 3 Dorothy Gale Judy Garland 3 The Wizard of Oz 1939

1 1 Han Solo Harrison Ford 4 Indiana Jones and the 1981


Raiders of the Lost Ark
4 1 Indiana Jones Harrison Ford 4 Indiana Jones and the 1981
Raiders of the Lost Ark
2 2 Ilsa Lund Ingrid 4 Indiana Jones and the 1981
Bergman Raiders of the Lost Ark
3 3 Dorothy Gale Judy Garland 4 Indiana Jones and the 1981
Raiders of the Lost Ark 78
Only keep tuples where title =
name
1 StarID Character Name 5 Title Year

1 1 Han Solo Harrison Ford 1 Star Wars 1977

2 2 Ilsa Lund Ingrid 2 Casablanca 1942


Bergman
3 3 Dorothy Gale Judy Garland 3 The Wizard of Oz 1939

4 1 Indiana Jones Harrison Ford 4 Indiana Jones and the 1981


Raiders of the Lost Ark

1 StarID Character Name 5 Title Year

Projection only keeps


the Name attribute
Name

79
Remember this one?
Find the names of all Movie Stars who
were in any Movie

pname(MovieStar ⋈ StarsIn)
Name
Harrison Ford
Ingrid Bergman
Judy Garland

What if we wanted all Movies Stars who


were in all movies?
80
Division
Notation: r / s or r ÷ s
Useful for expressing queries that include a
notion of “for all” or “for every”, e.g., Find movie
stars who were in all movies.
Let r and s be relations on schemas R and S
respectively where
r = (A1, …, Am, B1, …, Bn)
s = (B1, …, Bn)
Then r / s is a relation on schema
r / s = (A1, …, Am)
defined as
r / s = { t | t Î Õ r-s(r) Ù " u Î s ( tu Î r ) }
i.e., A/B contains all x tuples (MovieStars) such
that for every y tuple (movies) in B, there is an x,y 81
tuple in A.
Examples of Division A/B
A B1 B2 B3
sno pno pno pno pno
s1 p1 p2 p2 p1
s1 p2 p4 p2
s1 p3
A/B1 p4
s1 p4
s2 p1 sno A/B2
s2 p2 s1
s3 p2 sno A/B3
s2
s4 p2 s1 sno
s3
s4 p4 s4 s1
s4
82
Division Clicker Question
Consider the relations
A B ???
R C D E S T
1 2 1 1 2 2
2 2 1 2 2
3 2 1 3 2
1 1
Which of the following is a possible expression for creating T?
R(C,D) means !!" "
A. X(D)ß πAS
R(C,D)/X
B. Y(A)ßπCR
S(B,A)/Y
C. Z(C)ßπAS
R(E,C)/Z
D. All of the above
E. None of the above
83
Division Clicker Question
Consider the relations
A B ???
R C D E S T
1 2 1 1 2 2
2 2 1 2 2
3 2 1 3 2
1 1
Which of the following is a possible expression for creating T?
A. X(D)ß πAS
nothing
R(C,D)/X
B. Y(A)ßπCR right
S(B,A)/Y
C. Z(C)ßπAS No, 1
R(E,C)/Z
D. All of the above
84
E. None of the above
Division Clicker Question
Answer A exposed
A B ???
R C D E S T
1 2 1 1 2 2
2 2 1 2 2
3 2 1 3 2
1 1
Which of the following is a possible expression for creating T?
A. X(D)ß πAS
C D D
R(C,D)/X
1 2 1
B. Y(A)ßπCR
2 2 2
S(B,A)/Y
3 2 3
C. Z(C)ßπAS
R(E,C)/Z Result is
D. All of the above empty!
85
E. None of the above
Division Clicker Question
Answer B exposed
A B ???
R C D E S T
1 2 1 1 2 2
2 2 1 2 2
3 2 1 3 2
1 1
Which of the following is a possible expression for creating T?
A. X(D)ß πAS
B A A
R(C,D)/X
2 1 1
B. Y(A)ßπCR
2 2 2
S(B,A)/Y
2 3 3
C. Z(C)ßπAS 1 1
R(E,C)/Z Result is 2
D. All of the above
86
E. None of the above
Division Clicker Question
Answer C exposed
A B ???
R C D E S T
1 2 1 1 2 2
2 2 1 2 2
3 2 1 3 2
1 1
Which of the following is a possible expression for creating T?
A. X(D)ß πAS
E C C
R(C,D)/X
1 1 1
B. Y(A)ßπCR
1 2 2
S(B,A)/Y
1 3 3
C. Z(C)ßπAS
R(E,C)/Z Result is 1
D. All of the above
87
E. None of the above
Find the name of actors who have
been in all movies
Be careful in choosing the input relations!
InAllß p StarID, MovieIDStarsIn/ p MovieID(Movie)
pName(InAll ⋈ MovieStar)

88
Find the names of actors who have
been in all movies after 1950

LateMovieIds ß π MovieID(σ year > 1950(Movie))

InAllß (πStarID, MovieID (StarsIn)/ LateMovieIds)

πName(InAll ⋈MovieStar)

89
Case study of complex relational algebra:
build up division of r/s from other operators
Let X be attributes not in R and Y be
attributes in S
Idea: compute all values that are
“disqualified” by some value in s.
value x is disqualified if by attaching y value from
s, we obtain an xy tuple that is not in r.
Take difference from all values
Expressing r/s Using Basic Operators
Like a join, can be computed from basic operators
Idea:
let X the set of attributes of r that are not in s
(1) compute the X-projection of r
(2) compute all X-projection values of r that are
“disqualified” by some value in s.
value x is disqualified if by attaching y value from s, we
obtain an xy tuple that is not in r.
result is (1)-(2)
So,
Disqualified x values: p((p (r)´s)-r)
X X
r/s is p (r) -p ((p (r)´s)-r)
X X X
Example: building up division
subtract off disqualified answers
A=R B2 = S πX(R) πX(R) x S πX(R) x S -R
X Y Y X X Y X Y
S1 P1 P2 S1 S1 P2 S2 P4
S1 P2 P4 S2 S1 P4 S3 P4
S1 P3 S3 S2 P2
S1 P4 S4 S2 P4 These values
S2 P1 S3 P2 aren’t in R
S2 P2 S3 P4
S3 P2 All possible S4 P2 Values needed
S4 P2 values given R S4 P4 for πX(R)
S4 P4
πX(R) - πX(πX(R) x S – R) = A/B2
X
A/B2 = S1 Answers not disqualified
S4
If you want to practice…
Try this site:
[Link]

You can play with RA queries and see the


results.

93
Learning Goals Revisited
Identify the basic operators in RA.
Use RA to create queries that include
combining RA operators.
Given an RA query and table schemas
and instances, compute the result of the
query.

94
Relational Algebra In-Class 2
See Canvas for the assignment

95

You might also like