Relational Algebra
Relational Algebra
Basic relational algebra operators:
- Unary operators: selection , projection , renaming
- Binary operators: cross-product , union , intersect , difference
Closure property: Relations are closed under relational operators. Operators can be composed to form relational algebra expressions
Selection
selects tuples from relation R that satisfy the selection condition c
- iff c evaluates to true on t
- The output table has the same schema as R
Selection condition is a boolean combination of terms. A term is one of the following forms:
- attribute op constant (op ∈ {=, <>, <, ≤, >, ≥})
- attribute1 op attribute2
- term1 and term2
- term1 or term2
- not term1
- (term1)
Operator precedence: (), op, not, and, or
Selection conditions with null values:
- Result of a comparison operation involving
nullvalue is unknown - Result of an arithmetic operation involving
nullvalue isnull - Three-valued logic system: true, false, & unknown
| x | y | x AND y | x OR y | NOT x |
|---|---|---|---|---|
| FALSE | FALSE | FALSE | FALSE | TRUE |
| FALSE | UNKNOWN | FALSE | UNKNOWN | TRUE |
| FALSE | TRUE | FALSE | TRUE | TRUE |
| UNKNOWN | FALSE | FALSE | UNKNOWN | UNKNOWN |
| UNKNOWN | UNKNOWN | UNKNOWN | UNKNOWN | UNKNOWN |
| UNKNOWN | TRUE | UNKNOWN | TRUE | UNKNOWN |
| TRUE | FALSE | FALSE | TRUE | FALSE |
| TRUE | UNKNOWN | UNKNOWN | TRUE | FALSE |
| TRUE | TRUE | TRUE | TRUE | FALSE |
Projection
projects attributes given by a list of attributes from relation R
- Each attribute in must be an attribute in R
- The schema of the output table is determined by
Duplicate records are removed in the output relation
Renaming
renames the attributes in R based on a list of attribute renamings
- The schema of the output table is the same as R except that some attributes are renamed based on
is a list of attribute renamings of the form
- Each renaming renames attribute to
- Each must be an attribute in R
- Each attribute in R can be renamed at most once
- The order of the attribute renamings in does not matter
Set Operations
Union: returns a relation containing all tuples that occur in R or S (or both)
Intersection: returns a relation containing all tuples that occur in both R and S
Set-difference: returns a relation containing all tuples in R but not in S
Union (), intersection (), and set-difference () operators require input relations to be union compatible
Union Compatibility
Two relations are union compatible if
- they have the same number of attributes, and
- the corresponding attributes have the same domains
Union compatible relations do not necessarily use the same attribute names
Cross Product
Consider relations R(A, B, C) and S(X, Y). Cross-product: R × S outputs a relation with schema (A, B, C, X, Y) defined as follows:
Cross-product operation is also known as cartesian product
Writing Relational Algebra Queries
A complex relational algebra (RA) query presented as a single lengthy expression can be unreadable. Two methods to improve readability of RA queries
- Method 1: Operator trees
- Method 2: Sequence of steps
Join Operators in Relational Algebra
A join operator combines cross-product, selection, and possibly projection operators
Inner Join (aka Join):
The inner join of R and S is defined as:
Natural Join:
The natural join of R and S is defined as:
where
- A = common attributes between R & S =
- c is
- is the list of attributes in R that are also in A, followed by the list of attributes in R that are not in A, and the list of attributes in S that are not in A
Dangling Tuples
A dangling tuple is a tuple in a join operand that does not participate in the join operation
Let attr(R) denote the list of attributes in the schema of R. We say that is a dangling tuple in R w.r.t. if
To preserve dangling tuples in the join result, use outer joins
Left/Right/Full Outer Joins
Let denote the set of dangling tuples in R w.r.t. . Thus
Let denote a n-component tuple of null values, where n = arity of relation R
The left outer join (left join) of R and S is defined as:
The right outer join (right join) of R and S is defined as:
The full outer join (full join) of R and S is defined as:
Natural Left/Right/Full Outer Joins
Natural left outer join of R & S:
Natural right outer join of R & S:
Natural full outer join of R & S:
where
- A = common attributes between R & S =
- c is
- is the list of attributes in R that are also in A, followed by the list of attributes in R that are not in A, and the list of attributes in S that are not in A
Relational Algebra Expressions (RAEs)
- A relation is a RAE
- If R is a RAE, then , , and are also RAEs
- If R and S are RAEs, then , , , and , , , , , , , and are also RAEs
- If R is a RAE, then (R) is also a RAE