Relational Calculus is a non-procedural query language (or declarative language). It uses mathematical predicate calculus (or first-order logic) instead of algebra.
⇒ Relational Calculus tells what to do but never explains how to do it.
⇒ Relational Calculus provides a description of the query to get the result, whereas
Relational Algebra gives the method to get the result.
When applied to databases, it comes in two flavors:
1. Tuple Relational Calculus (TRC):
⇒ Proposed by Codd in 1972.
⇒ Works on tuples (or rows).
2. Domain Relational Calculus (DRC):
⇒ Proposed by Lacroix and Pirotte in 1977.
⇒ Works on the domain of attributes (or columns).
Calculus has variables, constants, comparison operators, logical connectives, and quantifiers.
TRC: Variables range over tuples (similar to SQL).
DRC: Variables range over domain elements (similar to Query-By-Example (QBE)).
Expressions in the calculus are called formulas. The resulting tuple is an assignment of constants to variables that make the formula evaluate to true.
Tuple Relational Calculus is a non-procedural query language used for selecting tuples in a relation that satisfy the given condition (or predicate). The result of the relation can have one or more tuples.
A query in TRC is expressed as:
{t | P(t)}
Where t denotes the resulting tuple and P(t) denotes the predicate (or condition) used to fetch tuple t.
Result of Query: It is the set of all tuples t such that predicate P is true for t.
Notations used:
⇒ t is a tuple variable.
⇒ t[A] denotes the value of tuple t on attribute A.
⇒ t ∈ r denotes that tuple t is in relation r.
⇒ P is a formula similar to that of predicate calculus.
Predicate Calculus Formula:
1. Set of attributes and constants.
2. Set of comparison operators: e.g., <, ≤, =, ≠, >, ≥
3. Set of connectives: and (∧ ), or (∨), not (¬).
4. Implication (⇒): x ⇒ y , if x is true, then y is true (x ⇒ y = ¬x ∨ y) .
5. Quantifiers:
a).Existential Quantifiers (∃) and
b).Universal Quantifier (∀).
Existential Quantifiers (∃)∃t ∈ r(Q(t)) = "there exists" a tuple t in relation r such that predicate Q(t) is true.
Universal Quantifier (∀).
∀t ∈ r(P(t)) = P is true "for all" tuples t in relation r.
Free and Bound Variables:
⇒ The use of quantifiers ∃x and ∀x in a formula is said to bind x in the formula.
⇒ A variable that is not bound is free.
⇒ Let us revisit the definition of a query: {t ∣ P(t)}.
There is an important restriction: the variable t that appears to the left of '|' must be the only free variable in the formula P(t). In other words, all other tuple variables must be bound using a quantifier.
It selects the set of tuples t such that there exists a tuple s in a relation loan for which the values of t & s for the loan number attribute are equal and the value of s for the amount t attribute is greater than $1 200
Domain Relational Calculus is a non-procedural query language.
⇒ In Domain Relational Calculus, the records are filtered based on the domains.
⇒ DRC uses a list of attributes to be selected from a relation based on the condition (or predicate).
⇒ DRC is the same as TRC but differs by selecting the attributes rather than selecting whole tuples.
In DRC, each query is an expression of the form:
{⟨a1,a2,…,an⟩∣ P(⟨a1,a2,…,an⟩)}Where a1,a2,…,an represent domain variables and
P represents a predicate similar to that of predicate calculus.
Result of Query: It is the set of all tuples ⟨a1,a2,…,an⟩ such that predicate P is true for ⟨a1,a2,…,an ⟩tuples.
Predicate Calculus Formula:
Notations used:
⇒ ⟨a1,a2,…,an⟩ ∈ where r is a relation on n attributes and a1,a2,…,an are domain variables or domain constants.
⇒ P is a formula similar to that of predicate calculus.
Formula:
1. Set of domain variables and constants.
2. Set of comparison operators: e.g., <,≤,=,≠,>.
3. Set of connectives: and (∧), or (∨\), not (¬).
4. Implication (⇒): x ⇒y if x is true, then y is true ( x ⇒ y = ¬x ∨ y ).
5. Quantifiers: Existential Quantifiers (∃\) and Universal Quantifier (∀).
∃x(P(x)) and ∀x (P(x))
Where x is a free domain variable.