Relational Algebra Updated
Relational Algebra Updated
RELATIONAL ALGEBRA
Calculating with relations
o Expression examples:
2+5
((2 − x) × 5) + (y / z)
Classical Relational Algebra
o Domain: Finite relations o Operators:
1. (Set) Union
o Operands:
2. (Set) Difference
o Constants: All finite relations
3. (Set) Intersection
o Variables: Arbitrary finite 4. Projection
relations 5. Selection
«Relations are sets, so we can apply set-theoretic 6. Cartesian product
operators. However, we want the results to be (Cross Product or Cross Join)
relations (that is, homogeneous sets of tuples). It is
therefore meaningful to only apply union, intersection,
o Join (all the others)
difference to pairs of relations defined over the same o Renaming
attributes»
o Division
Before we look at Relational Algebra and its
operators
o Relational Algebra and its operators are there for at
least two reasons
o We will need the algebra, its operators and its laws
in query compilation & optimization (next weeks)
o The counterparts of the operations actually exist in
SQL (but not always implemented with the same
name)
Practical counterparts of the operators
o We will use examples from w3resource
(https://www.w3resource.com/) or w3schools
(https://www.w3schools.com/sql/)
o w3resource has tutorials and examples for 2003 standard ANSI SQL
(use that primarily), as well as MySQL, PostgreSQL, Oracle etc., and for
NoSQL, GraphQL and others that you will need later in this course (and
in life)
o w3schools let you “Try it Yourself” that can help understand (the green
button)
o There are very many SQL help & tutorials, also on each DBMS´ own site
Set Operations
o Union: R∪S
o Intersection: R∩S
o Difference: R−S
site topic
w3schools Database
w3schools WebDev
w3schools WebDev
w3resource Database
w3resource Database
RENAMING (!)
o "S(A1,A2,...,An)(R) renames R to a relation S with name S and
attributes A1, A2, ..., An
o NOTE that it would is usually wise to use some condition (at least so
that the result makes sense).
o In SQL, the cartesian product is a CROSS JOIN
The Joy of Joins
o There are in practice two types of joins:
o EQUI-joins
Joins that allow for conditions only with equality in the
condition
o NON-EQUI-joins
All the other joins that allow for comparisons other than
equality (all the other kind of conditions)
Natural Join
o R⋈S is the relation obtained from R and S by forming all
possible mergers of one tuple from R with one from S where
the tuples are to match all attributes with matching names
o Common attributes occur only once in the merged
attributes
o The resulting schema has the attributes in R followed by
those attributes in S that do not also occur in R
o Natural join is an EQUI-JOIN!
Dangling Tuple
o A dangling tuple is a tuple in one of the relations that has
no matching tuple in the other relations
o Dangling tuples are not represented in the result relation
after a join
foods
Natural Join R ⋈ S
Practical Example
company
Dangling?
Note:
(! A(R) × S) – R are those that do NOT satisfy the condition
Typical steps for computation of R(A,B) div S(B):
o Find out all possible combinations of S(B) with R(A) by computing
R(A) × S(B). Call it R1
o Subtract actual R(A,B) from R1. Call it R2
o A in R2 are those that are not associated with every value in S(B);
therefore R(A)-R2(A) gives us the A that are associated with all values
in S
o Take a look at the examples here:
o https://www.geeksforgeeks.org/sql-division/
o https://www.studytonight.com/dbms/division-operator.php
Division: Example of use
o Romutstyr (room equipment)
show the equipment that
exists
o Aktivitetskrav (for activity)
shows the kind of equipment
needed for a given activity
Room that covers all equipment needs
o Let R = Romutstyr (room equipment) and A = Aktivitetskrav (for
activity)
o Room that covers all equipment requirements for MUS1225-
hørelære (hearing training):
R div !utstyr("aktivitet = MUS1225-hørelære(A))
o Room that covers all equipment requirements for MUS1225,
i.e., both hørelære (hearing training) and musikkprosuksjon
(music production):
R div !utstyr("aktivitet LIKE ´MUS1225%´(A))
Result of the division
Other derivable operators
o R∩S = R – (R – S)
o which means that we don´t /001 ∩ (we can derive it)
o R ⋈θ S =3θ(R × S)
o which means that we don´t /001 ⋈θ
o R⋈S= 5L(3θ(R × S)),
where L is the list of of attributes in R followed by the attributes in
S that don´t occur in R, and θ is R.A1 = S.A1 AND ... AND R.Ak = S.Ak
der A1, ... , Ak are all the attributes that occur in both R and S
o which means that we don´t /001 ⋈ J
The minimal set of operators
o Operators in the set {∪, –, ", #, ×, %} can not be expressed
using the other operators in the set
o They are the minimal indpendent set of operators
o Not all algebraic laws that hold for Sets hold for Bags
Example: (R ∪ S) – T = (R – T) ∪ (S –T)
SELECT company.company_name,
company.company_id,
foods.company_id,
foods.item_name,
foods.item_unit
FROM company, foods
WHERE company.company_id =
foods.company_id(+);
Relations and rules of integrity
o We can express referential integrity, functional
dependencies and multi-value dependencies - and also
other classes of integrity rules - in relational algebra!
Examples of integrity rules in classical
relational algebra
o If E is an expression in relational algebra, then E=∅ is an integrity rule
that says that E does not have any tuples
o If E1 and E2 are expressions in relational algebra, then
E1 ⊆ E2 is an integrity rule that says that each tupple in E1 shall also
be in E2
o Note that E1 ⊆ E2 and E1 – E2= ∅ are equivalent.
Also E= ∅ og E ⊆ ∅. Thus, only one of the forms above is sufficient
o Strictly speaking, ∅ is not a relational algebra expression. We could
have written R – R instead (for an arbitrary relation R with same
schema as E)
Examples of integrity rules in classical
relational algebra
o Referential integrity: ”A is foreign key for S”, where B is primary key in S:
!("A(R)) ⊆ "B(S)
o FDs: ”A1 A2 ... An→B1 B2 ... Bm” in R:
%&('R1(R)×'R2(R))=∅
o where & is the expression
R1.A1 = R2.A1 AND ... AND R1.An =
R2.An AND (R1.B1 ≠ R2.B1 OR ... OR R1.Bm ≠ R2.Bm)
o Domain constraints:
% A≠’F’ AND A≠’M’(R) = ∅