In this lecture, we will discuss the first formal languages for the relational models: Relational Algebra
In this lecture, we will discuss the first formal languages for the relational models: Relational Algebra
Relational algebra
Relational Algebra (RA) can be viewed as a data manipulation language for relational model. It consists of several basic operations which is enable user to specify retrieval requests. RA is called a procedural language in which user need to specify how to retrieve the expected data.
Relational Algebra has the following components:
Operands: Relations or Variables that represent relations
Operators that map relations to relations
Rules for combining operands and operators to relational algebra expression
Rules for evaluating those expressions
Operations of relational algebra include the followings:
Union, Intersect, Set Difference, Cartesian Product are operations based on set theory
Select, Project, Join, Division are operations developed especially for relational databases.
Relational algebra operations from set theory
Definition: Two relations r(A1, A2, …, An) and s(B1, B2, …, Bn) are union compatible if they have the same degree n and dom(Ai) = dom(Bi) for 1 ≤ i ≤ n.
This mean two union compatible relations have the same number of attributes and each corresponding pair of attributes have the same domain
UNION Operation
The UNION operation combines two union compatible relations into a single relation via set union of sets of tuples.
Notation:
where r1(R) and r2(R)
Result size:
Result schema: R
Producing the result of UNION
Make a copy of relation r1
For each tuple t in relation r2, check whether it is in the result or not. If it is not already in the result then place it there.
Example:
INTERSECTION Operation
The INTERSECTION operation combines two union compatible relations into a single relation via set intersection of sets of tuples.
Notation:
where r1(R) and r2(R)
Result size:
Result schema: R
Producing the result of INTERSECTION
Initially, result set is empty
For each tuple t in relation r1, if t is in the relation r2 then place t in the result set.
Example
SET DIFFERENCE Operation
The DIFFERENCE operation finds the set of tuples that exist in one relation but do not occur in the other union compatible relation
Notation: r1 \ r2
where r1(R) and r2(R)
Result schema: R
Producing the result of the DIFFERENCE operation
Initially, result set is empty
For each tuple in r1, check whether it appear in r2 or not. If it does not then place it in the result set. Otherwise, ignores it
Example
CARTESIAN PRODUCT Operation
The PRODUCT operation combines information from two relations pairwise on tuples.
Notation: r x s
where r(R) and s(S)
Each tuple in the result contains all attributes in r and s, possibly with some fields renamed to avoid ambiguity. The result set contains all possible tuple that can be construct from one tuple in r and one tuple in s.
Result schema: If we have R(A1, A2, …, An) and S(B1, B2, …, Bm) then the list of attributes in Result is (A1, A2, …, An, B1, B2, …, Bm)
Result size:
Producing the result of PRODUCT operation:
For each tuple in r, form new tuples by pair it with each tuple in s