0% found this document useful (0 votes)
3 views36 pages

Algebra

Relational algebra pdf notes

Uploaded by

amanantony
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)
3 views36 pages

Algebra

Relational algebra pdf notes

Uploaded by

amanantony
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/ 36

Introduction to Databases, Fall 2003

IT University of Copenhagen

Lecture 7: Relational algebra and SQL

October 7, 2003

Lecturer: Rasmus Pagh


Today’s lecture

• Views (postponed from last lecture).


• Basics of relational algebra.
• Relational algebra on bags and commercial RDBMSs.
• More relational algebra (and SQL).
• Algebraic laws.

1
What you should remember from previously

In this lecture I will assume that you remember:


• The mathematical definition of a relation as a set of tuples.
• Projection and selection using SELECT-FROM-WHERE.
• Natural join.
• Nested SQL queries.

2
Next: Basics of relational algebra.
What is an algebra?

An algebra consists of a set of atomic operands, and a set of operators.


We can form algebraic expressions by applying operators to operands
(which can be atomic or expressions themselves).

Example:
In the algebra of arithmetic, the atomic operands are constants and
variables, and the operators are +, -, /, and ·.
Using these we can form expressions like ((x + 7)/(y − 3)) + x.

4
Relational algebra

Relational algebra has relations as atomic operands, and various


operations on relations (such as select and join) as operators.
It is the mathematical basis of SQL queries.

Example relational algebra expression:

σa≥5 (R1 ./ R2 ) ∪ R3

using the operators σa≥5 , ./, and ∪ on operands R1 , R2 , and R3 .

5
Why is relational algebra useful?

Top reasons why relational algebra is covered in most database textbooks:


1. It gives another view of SQL queries, and thus a better understanding.
2. It is used in query optimization (to learn about this, enroll for Advanced
Database Technology in spring 2004!)
3. It can be used for reasoning about relational queries and constraints.
4. It is the historical background of relational databases.

6
Recap of set notation

To describe the operators of relational algebra we will use mathematical


notation for describing sets (recall that a relation is a set of tuples).

The notation {X | Y } is used to describe “the set of all elements of the


form X that satisfy the condition Y ”.

Examples:
• The set of negative integers: {x | x ∈ Z (the set of integers), x < 0}.
• The set of two-tuples of strings:
{(x, y) | x is a string, and y is a string}.

7
Selection in relational algebra

Recall that selection is the operation of choosing the tuples in a relation


satisfying some condition.

In relational algebra, the operator σC is used for selection with condition C.

Formally, σC (R) = {t ∈ R | t satisfies C}. Thus:

σC (R)

corresponds in SQL to
SELECT *
FROM R
WHERE C

8
Projection in relational algebra

Recall that projection is the operation of choosing certain attributes of a


relation.

In relational algebra, the operator πA1 ,...,An is used for projection onto
attributes A1 , . . . , An .

Formally:

πA1 ,...,An (R) = {(a1 , a2 , . . . , an ) | there exists t ∈ R where for all i,


ai is the value of attribute Ai of t}

9
Projection in relational algebra and SQL

πA1 ,...,An (R)

corresponds in SQL to

SELECT A1 , . . . , An
FROM R

Note that projection is the operator we use to compute relation instances in


a decomposition.

10
Set operators in relational algebra

Since relations are sets, we can apply the standard set operators.
• Union: R1 ∪ R2 = {x | x ∈ R1 or x ∈ R2 }.
• Intersection: R1 ∩ R2 = {x | x ∈ R1 and x ∈ R2 }.
• Difference: R1 − R2 = {x | x ∈ R1 and x 6∈ R2 }.

In SQL, the above expressions correspond to, respectively:


• R1 UNION R2
• R1 INTERSECT R2
• R1 EXCEPT R2

11
Problem session (5 minutes)

Try to come up with a formal definition of the natural join operation, i.e.,
the join operation used to combine decomposed relation instances.

Suppose the relations to be joined are R1 (A1 , . . . , An , B1 , . . . , Bm ) and


R2 (A1 , . . . , An , C1 , . . . , Ck ).

12
Natural join in relational algebra

The join operation used when recombining decomposed relations is called


natural join, denoted by ./ in relational algebra.

Suppose we have relations R1 (A1 , . . . , An , B1 , . . . , Bm ) and


R2 (A1 , . . . , An , C1 , . . . , Ck ). Then formally:

R1 ./ R2 = {(a1 , . . . , an , b1 , . . . , bm , c1 , . . . , ck )
| there exists t ∈ R1 and u ∈ R2 with
values a1 , . . . , an on attributes A1 , . . . , An of t and u,
values b1 , . . . , bm on attributes B1 , . . . , Bm of t,
and values c1 , . . . , ck on attributes C1 , . . . , Ck of u}

13
Natural join in relational algebra and SQL

R1 ./ R2
corresponds in SQL to

R1 NATURAL JOIN R2

Natural join is the operator we use to recover the original relation in a


decomposition.

Note: NATURAL JOIN is not supported in Oracle 8i installed at ITU.

14
Next: Relational algebra on bags and commercial RDBMSs.
Relational algebra vs SQL

In all the cases we saw, the correspondence between relational


algebra and SQL queries is not what you might think!

Let’s look at some live examples in Oracle. . .

16
Relations in SQL are bags

What we have seen is that relations in SQL are bags (or multisets), i.e.,
tuples may appear more than once.

The fact that the same attribute may occur several times is a different (and
less important) issue that we won’t go into.

It is possible to define relational algebra on bags, whose operators are


basically identical to those of SQL.

17
Features of relational algebra on bags
Relational algebra on bags is basically the same as relational algebra (on
sets), without duplicate elimination.
• πA1 ,...,An (R) has one tuple for each tuple of R, even if the tuples
become identical when some attributes are removed.
• σC (R) contains all tuples of R satisfying C, including duplicates.
• A tuple occurs x · y times in R1 ./ R2 if it was formed by combining a
tuple occurring x times in R1 with a tuple occurring y times in R2 .
• R1 ∪ R2 contains all tuples of R1 and R2 , including duplicates.
(This corresponds to UNION ALL in SQL.)
• R1 ∩ R2 and R1 − R2 can also be defined – see book for details.
• A new duplicate elimination operator: δ(R) is the set of (different)
tuples occurring in the bag R.

18
Why bags?

The reason for using bags (rather than sets, which are easier to handle) is
database efficiency.

Since efficiency is crucial for commercial RDBMSs, SQL was carefully


designed to allow efficient evaluation of queries.

The reason why bags are used is that duplicate elimination is relatively
costly (requires time and memory), so it is generally an advantage to use it
only when necessary.

19
Duplicate elimination in SQL

We can force duplicate elimination in a SELECT-FROM-WHERE by adding the


keyword DISTINCT.

Example: To compute the relational algebra expression πA1 ,...,An (R) in


SQL, use SELECT DISTINCT A1 , . . . , An FROM R.

Some SQL operators, like UNION, INTERSECT, and EXCEPT, automatically


perform duplicate elimination.

If we always used DISTINCT etc., the semantics of SQL would match


relational algebra. However, when efficiency is an issue this is a bad idea.

20
Problem session (5-10 minutes)

Suppose that R and S are relations with one common attribute, A, and
consider the expression

((σC1 (R)) ∪ S) ./ (σC2 (R))

Write SQL expressions that are equivalent to the above:


1. When interpreted as an expression in relational algebra (on sets).
2. When interpreted as an expression in relational algebra on bags.

21
Next: More relational algebra (and SQL).
Other kinds of join

• Cartesian product. R1 × R2 , corresponds in SQL to


SELECT * FROM R1 , R2 .
• Theta-join (Θ-join). R1 ./ R2 , corresponds in SQL to
C
SELECT * FROM R1 , R2 WHERE C.

• Outerjoin. R1 ./ R2 , includes all tuples of R1 ./ R2 , and further
includes dangling tuples of R1 and R2 that are not matched with any
tuple of the other relation, padded with NULL values.
Corresponds in SQL to R1 FULL NATURAL OUTER JOIN R2 .
(This is not implemented in Oracle 8i installed at ITU.)

[Figure 5.19 shown on slide]

23
Aggregation operators

Aggregation operators are used to compute facts about the values of some
attribute in a relation.

The standard aggregation operators are: SUM, AVG, MIN, MAX, and COUNT,
computing respectively the sum, average, minimum, maximum and number
of the attribute values.

In relational algebra, the aggregation of attribute A in a relation R with


operator OP is written: γOP(A) (R)

Aggregation can be done in SQL by specifying the aggregation operator in


the SELECT clause:
SELECT OP(A)
FROM R

24
Grouping and aggregation

Aggregation is most useful in connection with grouping, where the tuples of


a relation are split into groups, for each of which the aggregate is computed.

The tuples in a relation are divided into groups based on the values of a
specified set of grouping attributes, and the aggregate is computed for
each group.

Aggregation of attribute A in a relation R with operator OP on grouping


attributes A1 , . . . , An is written in relational algebra as γA1 ,...,An ,OP(A) (R)

The SQL equivalent is


SELECT A1 , . . . , An , OP(A)
FROM R
GROUP BY A1 , . . . , An

25
Semantics of aggregation

When computing an aggregate, we get one tuple for each list of values of
the grouping attributes. In addition to the grouping attributes, the tuple
contains the aggregate value(s) for the group.

The formal definition of the grouping and aggregation operators in


relational algebra depends on the operator in question. For SUM we have:

γA1 ,...,An ,SUM(A) (R) = {(a1 , a2 , . . . , an , s) | (a1 , a2 , . . . , an ) ∈ πA1 ,...,An (R)


and s = Σt∈σA1 =a1 ,...,An =an (R) πA (t)}

26
Aggregate conditions on groups

Sometimes we wish to perform a selection of certain groups, based on an


aggregate value of that group.

SQL supports a convenient way of doing this (with no direct equivalent in


relational algebra):
SELECT <attributes and aggregates in the result>
FROM R
GROUP BY <grouping attributes>
HAVING <condition that may involve aggregates>

The HAVING clause may contain conditions like MIN(year) < 1930, where
MIN(year) is the minimum value of the year attribute within the group.

Note: This would make no sense in a WHERE clause. (Why?)

27
Next: Algebraic laws.
Laws in relational algebra

An algebraic law is an equation (or other mathematical statement) which is


always true in a particular algebra.

Using such laws we could, e.g., conclude that the following two relational
algebra expressions are equivalent:

((σC1 (R1 )) ∪ R2 ) ./ (σC2 (R1 ))

(σC1 AND C2 (R1 )) ∪ (σC2 (R1 ./ R2 ))

The laws of relational algebra allow us to:


• Reason about relational expressions (and thus SQL expressions).
• Reason about relational constraints (next lecture).
• Perform query optimization (next course).

29
Basic laws in relational algebra

Commutativity laws, examples


• R ./ S = S ./ R
• R∪S =S∪R
• R∩S =S∩R

Associativity laws, examples


• (R ./ S) ./ T = R ./ (S ./ T )
• (R ∪ S) ∪ T = R ∪ (S ∪ T )
• (R ∩ S) ∩ T = R ∩ (S ∩ T )

30
Problem session (5 minutes)

Argue that the equation

(R ./ S) ./ T = R ./ (S ./ T )

is indeed a valid algebraic law.

In other words: Argue that the equality holds for any relations R, S, and T .

31
Relational algebraic law school, continued

Distributive laws, examples


• (R ∩ S) ∪ T = (R ∪ T ) ∩ (S ∪ T )
• (R ∪ S) ./ T = (R ./ T ) ∪ (S ./ T )
• σC (R ∪ S) = σC (R) ∪ σC (S)
• πL (R ./ S) = πL (πL∪J (R) ./ πL∪J (S)), where J is the set of common
attributes of R and S.

Example of use: By the second law, the expression

((σC1 (R1 )) ∪ R2 ) ./ (σC2 (R1 ))

is equivalent to ((σC1 (R1 )) ./ (σC2 (R1 ))) ∪ (R2 ./ (σC2 (R1 ))).

32
An algebraic criterion for decomposition

We can decompose a relation R into R1 (A1 , . . . , An , B1 , . . . , Bm ) and


R2 (A1 , . . . , An , C1 , . . . , Ck ) exactly when we have the equality:

R = (πA1 ,...,An ,B1 ,...,Bm (R)) ./ (πA1 ,...,An ,C1 ,...,Ck (R))

This is the formal way of stating that the relation instances of R1 and R2 ,
derived from R by projection, should always join to form R.

33
Most important points in this lecture

As a minimum, you should after this week:


• Know the meaning of the most common relational algebra operators:
∪, ∩, −, π, σ, ×, ./, γ.
• Be able to translate simple SQL queries to relational algebra on bags,
and vice versa.
• Recognize relational algebra laws.

34
Next lecture (in two weeks)

Next time we will cover constraints and triggers in databases.


• Constraints are assertions that we want to be true at all times in the
database.
• Relational algebra can be used for expressing and reasoning about
constraints.
• Depending on the type of constraint, it is maintained by some form of
active element, which is an action that is carried out whenever a
database change might violate the constraint.
• SQL supports a powerful kind of active element, called a trigger.

35

You might also like