Fundamentals of Database systems
DSE 310
Relational Algebra and Relational
Calculus
Lecture Objectives:
● Understand relation algebra and relational calculus
● Understand different operators and their formal definitions
Topics:
● Basic operations ● Other operations
○ Select ○ Intersection
○ Project ○ Join
○ Union ○ Aggregate
○ Set Difference ○ Division
○ Cartesian product
○ Rename
Relational algebra is procedural but calculus is non procedural
(In NPTEL both are mentioned as non-procedural)
Few Points on Relational Algebra
● Procedural and algebra based
● Proposed by Edgar Codd in 1970s
● Relations are set of tuples defined by a list of attributes.
● Input to each query (relational algebra expression) is a table or set of tables
● Query output is a table
● All data in the output table appears in one of the input tables.
● Procedural language
● Not turing equivalent language
○ Everything that can be computed may not be computed by this language
Select operation -> Selection of rows/tuples
Project operation -> Selection of attributes/columns
ΠA,C (r) = {t.A, t.C | t ϵ r }
Union operation -> Union of relations with same attributes
Set difference operation
Set Intersection operation
Join: Cartesian product
Rename operation
Natural Join
Aggregate Operators
● Usually applied over a column to get aggregate value
○ SUM
○ AVG
○ MAX
○ MIN
Division Operation
● R(Z) / S(X)
● Numerator R(Z): R is a relation with Z set of attributes
● Denominator S(X): S is a relation with X set of attributes
● We can calculate R(Z) / S(X) if and only if X is a subset of Z.
● Let Y = Z - X i.e. set of attributes present in Z but not in X
● Then R(Z)/ S(X) will be a relation T that will contain only Y attributes
● So R(Z)/ S(X) will result T(Y)
● T(Y) will have a tuple t if
○ t = tR (Y) where tR is a tuple from R(Z) relation
○ And for every tuple ts in S(X) we have a tR in R(Z) such that ts = tR (X)
tR (Y) will return tuple( Y attribute projections of tR )
Division example
Division: another example
Note
Relational calculus
● Non-procedural
○ Mentions what we want as result
○ Doesn’t mention how to get them
● Two versions
○ Tuple relational calculus
■ Operates on tuples of relation(s)
○ Domain relational calculus
■ Operates on domains/ attributes of relation(s)
● Both are Predicate calculus based
Predicate calculus
● Operates on variables and constants
● Set of comparison operators (e.g. <, ≤ , =, ≠, >, ≥)
● Set of connectives: or(∨), and(∧), not(¬)
● Implication (=>): If x is true, then y is true. ( x=>y is same as ¬x∨y )
● Set of quantifiers
Tuple Relational calculus
● Queries are in the form { t | P(t) }
The output is a set of all tuples such that for each tuple t, P(t) is true.
Where t is called tuple variable and t[A] is the value of tuple t on attribute A.
● The expression t ϵ r, says tuple t belongs to relation r
● P is the formula similar to predicate calculus
Domain relational calculus
● Non-procedural and equivalent in power to tuple relational calculus
● The terms are written in the form of
Equivalence of RA, TRC and DRC
Equivalence of RA, TRC and DRC
Equivalence of RA, TRC and DRC
Equivalence of RA, TRC and DRC
Equivalence of RA, TRC and DRC
Safety of relations
Thank you