0% found this document useful (0 votes)
95 views7 pages

Relational Algebra

Relational algebra is a procedural language used to query and manipulate relations in a relational database. It uses operations like selection, projection, union, intersection, difference, Cartesian product, and joins. Relational calculus uses logic and is not procedural. A query expresses a relational algebra operation to retrieve relational data by applying one or more operations to a relation.

Uploaded by

dnlkaba
Copyright
© Attribution Non-Commercial (BY-NC)
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)
95 views7 pages

Relational Algebra

Relational algebra is a procedural language used to query and manipulate relations in a relational database. It uses operations like selection, projection, union, intersection, difference, Cartesian product, and joins. Relational calculus uses logic and is not procedural. A query expresses a relational algebra operation to retrieve relational data by applying one or more operations to a relation.

Uploaded by

dnlkaba
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 7

V22.

0380: Database & Web Programming

Deena Engel

Notes: Relational Algebra


Relational algebra is a procedural language. (A procedural language is one in
which the steps of operation are described or outlined within the language expressions.)

Relational calculus uses logic and is not a procedural language.


A query is an expression of relational algebra. Operations in relational algebra fall into four categories: 1. Set operations (such as union, intersection, and difference) can be applied to relations in relational algebra. 2. Operations can be used to remove parts of a relation: for example, selection removes or singles out certain rows while projection removes or singles out certain columns. 3. Operations include joins (e.g. Cartesian product) in which pairs are combined. 4. Renaming is an operation which does not change the relations among the tuples but does change the schema (i.e. changes to field and/or table names).

Page 1 of 7

[email protected]

March 31, 2003

V22.0380: Database & Web Programming

Deena Engel

Examples of set operations used in relational algebra:


Given the following two tables:

Set R Painting title


Starry Night Self Portrait

Artist
Van Gogh Rembrandt

Medium
Oil on canvas Oil on canvas

Museum
MoMA Metropolitan Museum

Set S Painting title


Night Hawks Self Portrait

Artist
Hopper Rembrandt

Medium
Oil on canvas Oil on canvas

Museum
Whitney Metropolitan Museum

The union of R and S, R S, is:

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 intersection of R and S, R S, is:

Painting title
Self Portrait

Artist
Rembrandt

Medium
Oil on canvas

Museum
Metropolitan Museum

The difference of R and S, R S, is:

Painting title
Starry Night

Artist
Van Gogh

Medium
Oil on canvas

Museum
MoMA

Page 2 of 7

[email protected]

March 31, 2003

V22.0380: Database & Web Programming

Deena Engel

Using Projection in relational algebra:

Projection is an operation to select specific columns.


Given the following table:

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

[email protected]

March 31, 2003

V22.0380: Database & Web Programming

Deena Engel

Using Selection in relational algebra:

Selection is an operation to select specific rows.


Given the following table:

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

Likewise, the expression:

artist= Rembrandt AND museum = Van Rijks Museum(Paintings)


would be evaluated to:

Painting title
Portrait of a Lady

Artist
Rembrandt

Medium
Oil on canvas

Museum
Van Rijks Museum

Page 4 of 7

[email protected]

March 31, 2003

V22.0380: Database & Web Programming

Deena Engel

Products and Joins


The Cartesian Product of two sets R and S is the set of pairs that can be formed by choosing the first element of the pair in R to any element in S and is denoted by R x S. The result is a MUCH larger table! This is something to be careful of. Here is how it works: Suppose relation R consists of:

A
1 3

B
2 4

And relation S consists of:

B
2 4 9

C
5 7 10

D
6 8 11

The result of R x S would contain:

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

[email protected]

March 31, 2003

V22.0380: Database & Web Programming

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

medium= oil on canvas

Paintings The resulting table would include:

Paintings

Painting_title

Museum

Self Portrait Portrait of a Lady

Metropolitan Museum Van Rijks Museum

Page 6 of 7

[email protected]

March 31, 2003

V22.0380: Database & Web Programming

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

[email protected]

March 31, 2003

You might also like