Relational Algebra: Lecture Outline
Relational Algebra: Lecture Outline
Relational Algebra: Lecture Outline
Lecture Outline
Relational Algebra
Relational algebra operations work on one or more relations to define another relation without
changing the original relations.
Both operands and results are relations, so output from one operation can become input to
another operation.
Allows expressions to be nested, just as in arithmetic. This property is called closure.
Relational Algebra
σpredicate (R)
Works on a single relation R
Defines a relation that contains only those tuples (rows) of R that satisfy the specified
condition (predicate).
The predicate can be =, , ≥, <>
Projection
Πcol1, . . . , coln(R)
Works on a single relation R
Defines a relation that contains a vertical subset of R, extracting the values of specified
attributes and eliminating duplicates.
Union
R∪S
Union of two relations R and S defines a relation that contains all the tuples of R, or S, or
both R and S, duplicate tuples being eliminated.
R and S must be union-compatible.
If R and S have I and J tuples, respectively, union is obtained by concatenating them into one
relation with a maximum of (I + J) tuples.
Set Difference
R–S
Defines a relation consisting of the tuples that are in relation R, but not in S.
R and S must be union-compatible.
Intersection
R∩S
Defines a relation consisting of the set of all tuples that are in both R and S.
R and S must be union-compatible.
Expressed using basic operations:
R ∩ S = R – (R – S)
Cartesian product
RXS
Defines a relation that is the concatenation of every tuple of relation R with every tuple of
relation S.
Join Operations
Join Operations
R FS
Defines a relation that contains tuples satisfying the predicate F from the Cartesian product
of R and S.
The predicate F is of the form R.ai θ S.bi where θ may be one of the comparison operators (,
≥, =, ≠).
Can rewrite Theta join using basic Selection and Cartesian product operations.
R FS = σF(R Χ S)
Degree of a Theta join is sum of degrees of the operand relations R and S. If predicate F contains
only equality (=), the term Equijoin is used.
Natural join
RS
An Equijoin of the two relations R and S over all common attributes x.
One occurrence of each common attribute is eliminated from the result.
Outer join
To display rows in the result that do not have matching values in the join column, use Outer join.
RS
(Left) outer join is join in which tuples from R that do not have matching values in common
columns of S are also included in result relation.
Semijoin
RFS
Defines a relation that contains the tuples of R that participate in the join of R with S.
Can rewrite Semijoin using Projection and Join:
R F S = ΠA(R F S)