Dr. Amiya Ranjan Panda
Assistant Professor [II]
School of Computer Engineering,
Kalinga Institute of Industrial Technology (KIIT),
Deemed to be University,Odisha
Relational Calculus
KALINGA INSTITUTE OF INDUSTRIAL
TECHNOLOGY
School Of Computer
Engineering
4 Credit Lecture Note 14
Chapter Contents
q Relational Calculus
q Tuple Relational Calculus
q Safe Expressions
q Queries
q Domain Relational Calculus
q Queries
2
Ø Relational calculus is non-procedural.
Ø In relational calculus, a query is solved by defining a solution relation in a
single step.
Ø Relational calculus is mainly based on the well-known propositional calculus,
which is a method of calculating with sentences or declarations.
Ø Various types of relational calculus are:
ü Tuple Relational Calculus (TRC)
ü Domain Relational Calculus (DRC)
3
Relational Calculus
Ø A tuple variable is a variable that takes on tuples of a particular relation schema
as values.
Ø A tuple relational calculus query has the form: {T/ P(T)}
Ø The result of this query is the set of all tuples t for which the formula P(T)
evaluates to TRUE with T = t
Sailors (sid, sname, rating, age)
Query: Find all the sailors with a rating above 4
{S/S ∈ Sailors ∧ S.rating > 4}
4
Tuple Relational Calculus
Ø Let Rel → be a relation name, R, S → be the tuple variables, a → an attribute
of R, b → an attribute of S, op → operator in the set {<, ≤, >, ≥, =, ^=}. An
Atomic formula is one of the following:
ü R ∈ Rel
ü R.a op S.b
ü R.a op Constant or Constant op R.a
Ø To represent the join and division of relational algebra by relational calculus,
we need quantifiers such as: existential for join and universal for division.
Ø A quantifier quantifies or indicates the quantity of something.
Ø The existential quantifier (∃) states that at least one instance of a particular
type of thing exist Similarly, the universal quantifier (∀) states that some
condition applies to all or to every row of some type.
Ø A formula is recursively defined by using the following rules:
ü Any atomic formula
ü If p and q are formulae, then ¬ p, p ∧ q, p ∨ q, or p ⇒ q are also
formulae
ü If p is a formula that contains T as a variable, then ∃ T(p) and ∀ T(p) are
also formulae.
5
Tuple Relational Calculus
Ø The quantifiers ∃ and ∀ are said to bind the tuple variable R; whereas a
variable is said to be free in a formula if the formula does not contain an
occurrence of a quantifier that binds it.
Ø In most of the queries, the output is shown by using the free variables.
Ø Safe Expressions: Whenever we use universal quantifiers or existential
quantifiers in a calculus expression, we must make sure that the resulting
expression makes sense.
Ø A safe expression in relational calculus is one that is guaranteed to yield a
finite number of tuples as its result; otherwise, the expression is called unsafe.
Ø That means, an expression is said to be safe if all values in its result are from
the domain of the expression.
6
Tuple Relational Calculus
7
Queries
8
Queries…
9
Queries…
Ø In tuple relational calculus, the variables range over the tuples whereas in
domain relational calculus, the variables range over the domains.
Ø Let Rel → be a relation name, X, Y → be the domain variables, op → an
operator in the set {<, ≤, >, ≥, =, ^=}. An Atomic formula in domain relational
calculus is one of the following:
ü <x1, x2, ... xn > ∈ Rel
ü X op Y
ü X op Constant or Constant op X
Ø A formula is recursively defined by using the following rules:
üAny atomic formula
üIf p and q are formulae, then ¬p, p ∧ q, p ∨ q, or p ⇒ q are also formulae
üIf p is a formula that contains X as a domain variable, then ∃X(p) and ∀
X(p) are also formulae
Ø The quantifiers ∃ & ∀ are said to bind the domain variable X. Whereas a
variable is said to be free in a formula if the formula does not contain an
occurrence of a quantifier that binds it.
10
Domain Relational Calculus (DRC)
11
Queries
12
Queries…
13

Dbms 14: Relational Calculus

  • 1.
    Dr. Amiya RanjanPanda Assistant Professor [II] School of Computer Engineering, Kalinga Institute of Industrial Technology (KIIT), Deemed to be University,Odisha Relational Calculus KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY School Of Computer Engineering 4 Credit Lecture Note 14
  • 2.
    Chapter Contents q RelationalCalculus q Tuple Relational Calculus q Safe Expressions q Queries q Domain Relational Calculus q Queries 2
  • 3.
    Ø Relational calculusis non-procedural. Ø In relational calculus, a query is solved by defining a solution relation in a single step. Ø Relational calculus is mainly based on the well-known propositional calculus, which is a method of calculating with sentences or declarations. Ø Various types of relational calculus are: ü Tuple Relational Calculus (TRC) ü Domain Relational Calculus (DRC) 3 Relational Calculus
  • 4.
    Ø A tuplevariable is a variable that takes on tuples of a particular relation schema as values. Ø A tuple relational calculus query has the form: {T/ P(T)} Ø The result of this query is the set of all tuples t for which the formula P(T) evaluates to TRUE with T = t Sailors (sid, sname, rating, age) Query: Find all the sailors with a rating above 4 {S/S ∈ Sailors ∧ S.rating > 4} 4 Tuple Relational Calculus
  • 5.
    Ø Let Rel→ be a relation name, R, S → be the tuple variables, a → an attribute of R, b → an attribute of S, op → operator in the set {<, ≤, >, ≥, =, ^=}. An Atomic formula is one of the following: ü R ∈ Rel ü R.a op S.b ü R.a op Constant or Constant op R.a Ø To represent the join and division of relational algebra by relational calculus, we need quantifiers such as: existential for join and universal for division. Ø A quantifier quantifies or indicates the quantity of something. Ø The existential quantifier (∃) states that at least one instance of a particular type of thing exist Similarly, the universal quantifier (∀) states that some condition applies to all or to every row of some type. Ø A formula is recursively defined by using the following rules: ü Any atomic formula ü If p and q are formulae, then ¬ p, p ∧ q, p ∨ q, or p ⇒ q are also formulae ü If p is a formula that contains T as a variable, then ∃ T(p) and ∀ T(p) are also formulae. 5 Tuple Relational Calculus
  • 6.
    Ø The quantifiers∃ and ∀ are said to bind the tuple variable R; whereas a variable is said to be free in a formula if the formula does not contain an occurrence of a quantifier that binds it. Ø In most of the queries, the output is shown by using the free variables. Ø Safe Expressions: Whenever we use universal quantifiers or existential quantifiers in a calculus expression, we must make sure that the resulting expression makes sense. Ø A safe expression in relational calculus is one that is guaranteed to yield a finite number of tuples as its result; otherwise, the expression is called unsafe. Ø That means, an expression is said to be safe if all values in its result are from the domain of the expression. 6 Tuple Relational Calculus
  • 7.
  • 8.
  • 9.
  • 10.
    Ø In tuplerelational calculus, the variables range over the tuples whereas in domain relational calculus, the variables range over the domains. Ø Let Rel → be a relation name, X, Y → be the domain variables, op → an operator in the set {<, ≤, >, ≥, =, ^=}. An Atomic formula in domain relational calculus is one of the following: ü <x1, x2, ... xn > ∈ Rel ü X op Y ü X op Constant or Constant op X Ø A formula is recursively defined by using the following rules: üAny atomic formula üIf p and q are formulae, then ¬p, p ∧ q, p ∨ q, or p ⇒ q are also formulae üIf p is a formula that contains X as a domain variable, then ∃X(p) and ∀ X(p) are also formulae Ø The quantifiers ∃ & ∀ are said to bind the domain variable X. Whereas a variable is said to be free in a formula if the formula does not contain an occurrence of a quantifier that binds it. 10 Domain Relational Calculus (DRC)
  • 11.
  • 12.
  • 13.