Relational Algebra
Relational Algebra
Deena Engel
Page 1 of 7
Deena Engel
Artist
Van Gogh Rembrandt
Medium
Oil on canvas Oil on canvas
Museum
MoMA Metropolitan Museum
Artist
Hopper Rembrandt
Medium
Oil on canvas Oil on canvas
Museum
Whitney Metropolitan Museum
Painting title
Starry Night Self Portrait Night Hawks
Artist
Van Gogh Rembrandt Hopper
Medium
Oil on canvas Oil on canvas Oil on canvas
Museum
MoMA Metropolitan Museum Whitney
Painting title
Self Portrait
Artist
Rembrandt
Medium
Oil on canvas
Museum
Metropolitan Museum
Painting title
Starry Night
Artist
Van Gogh
Medium
Oil on canvas
Museum
MoMA
Page 2 of 7
Deena Engel
Painting title
Starry Night Self Portrait Night Hawks
Artist
Van Gogh Rembrandt Hopper
Medium
Oil on canvas Oil on canvas Oil on canvas
Museum
MoMA Metropolitan Museum Whitney
The columns in the relation R above (which we refer to as a table) can be represented as: A1, A2, ,An(R) which in this case could be written as: painting_title, artists, medium, museum(Paintings)
Projection would select only certain columns, and would be written as:
painting_title, artist (Paintings) The resulting relation is
Painting title
Starry Night Self Portrait Night Hawks
Artist
Van Gogh Rembrandt Hopper
Page 3 of 7
Deena Engel
Painting title
Starry Night Self Portrait Night Hawks Portrait of a Lady The expression:
Artist
Van Gogh Rembrandt Hopper Rembrandt
Medium
Oil Oil Oil Oil on on on on canvas canvas canvas canvas
Museum
MoMA Metropolitan Museum Whitney Van Rijks Museum
artist= Rembrandt(Paintings)
would be evaluated to:
Painting title
Self Portrait Portrait of a Lady
Artist
Rembrandt Rembrandt
Medium
Oil on canvas Oil on canvas
Museum
Metropolitan Museum Van Rijks Museum
Painting title
Portrait of a Lady
Artist
Rembrandt
Medium
Oil on canvas
Museum
Van Rijks Museum
Page 4 of 7
Deena Engel
A
1 3
B
2 4
B
2 4 9
C
5 7 10
D
6 8 11
A
1 1 1 3 3 3
R.B
2 2 2 4 4 4
S.B
2 4 9 2 4 9
C
5 7 10 5 7 10
D
6 8 11 6 8 11
There are many kinds of joins. Natural joins for example are used when there are components in R and S that match in some way. So the natural join of R and S would evaluate to:
A
1 3
B
2 4
C
5 7
D
6 8
Page 5 of 7
Deena Engel
Renaming
Columns and relations can be renamed without altering the results.
Writing Queries
Relational algebra, like all algebras, allows us to combine operations and form expressions of arbitrary complexity by applying the operators to either relations or relations that are the result of other operations. Sometimes, these are drawn as expression trees: For example, to answer the question, What is the title and which museum houses of all paintings which were painted by Rembrandt and done as oil on canvas?, there are several steps: (1) Select the painting tuples where artist = Rembrandt (2) Select the painting tuples where medium = oil on canvas (3) Compute the intersection of (1) and (2) (4) Project the relation from (3) onto attributes title and museum
title, museum
artist= Rembrandt
Paintings
Painting_title
Museum
Page 6 of 7
Deena Engel
Additional Operations
There are some additional operations that appear in query languages such as SQL but which are not part of the relational algebra model: 1. Modifications: SQL queries can be written to insert, delete, and update data. 2. Aggregations: SQL queries can be written to sum, average, or otherwise calculate aggregate values based on specified data. (Aggregation operators generally include count, sum, average, minimum, and maximum among others.) 3. SQL languages accept NULL values for unknown, hidden, or inapplicable components.
Page 7 of 7