SlideShare a Scribd company logo
Lect III B Relational Calculus
What will be covered
• Introduction
• Why is it called Relational calculus
• Common Notations Used in Relational Calculus
• Types of Relational Calculus
• Examples
• Comparison between TRC & DRC
Introduction: Relational
Calculus
• Relational Calculus in DBMS is just another way of
formulating queries. Relational calculus is a non-
procedural query language that tells the system
what data to be retrieved but doesn’t tell how to
retrieve it. The relational calculus tells what to do
but never explains how to do. Most commercial
relational languages are based on aspects of
relational calculus including SQL-QBE and QUEL.
Why is it called Relational Calculus?
• It is called Relational Calculus because it is used to manipulate
and describe relational data in DBMS using a mathematical and
logical approach. The “relational” part refers to representing and
organizing the data in a tabular form with the help of relations or
tables. The “calculus” part signifies a system of calculation and
reasoning.
• Relational Calculus is based on predicate calculus, a truth-valued
function with arguments. When the values of the arguments are
substituted in the predicate calculus, the resulting expression is
called a proposition which can be either true or false.
• Relational calculus is a tailored version of a subset of Predicate
Calculus, which helps communicate with the relational database.
Common Notations Used in Relational Calculus
• Comparison operators =, <>, <, >,
• Logical connectives
∧ - represents AND.
∨ - represents OR.
¬ - represents NOT.
∈ - represents Belongs to.
=>- implies
• Quantifiers (Universal Quantifier).
• ∀ - (for all): All tuples satisfy the condition.
• represents that in a set of tuples, all of them satisfy
the condition.
• ∃ - (exists): There exists at least one tuple satisfying the
condition.
represents that in a set of tuples, there is at least
Set Operations:
∪: Union
∩: Intersection
-: Difference
Additional Symbols:
π: Projection (selecting specific attributes)
σ: Selection (filtering tuples based on conditions)
ρ: Renaming (changing attribute names)
Types of Relational Calculus
TRC vs DRC
•TRC: Variables range over (i.e., get bound to)
tuples.
•DRC: Variables range over domain elements (=
attribute values)
•Both TRC and DRC are subsets of first-order
logic We use some short-hand notation to
simplify formulas
•Expressions in the calculus are called formulas
•Answer tuple = assignment of constants to
variables that make the formula evaluate to
true
Tuple Relational Calculus (TRC)
• TRC a non-procedural query language used in relational database
management systems (RDBMS) to retrieve data from tables. TRC
is based on the concept of tuples, which are ordered sets of
attribute values that represent a single row or record in a
database table.
• It is a non-procedural query language with which we find tuples
that hold true for a given condition. These given conditions are
called predicates. Tuple Relational Calculus describes the desired
information without providing specific information for obtaining
that information, i.e., specifies ‘what’ but not ‘how.’
• It goes to each table row and checks whether the predicate
condition is satisfied (true) or not (false). It returns the tuple(s)
which hold true for the predicate condition.
• TRC is a declarative language, meaning that it specifies what data
is required from the database, rather than how to retrieve it. TRC
queries are expressed as logical formulas that describe the desired
tuples. Syntax: The basic syntax of TRC is as follows: {t | P(t)}
• where t is a tuple variable or resulting tuples
• P(t) is a logical formula that describes the conditions that the
tuples in the result must satisfy.
• P(t) = known as Predicate and these are the conditions that are
used to fetch t. Thus, it generates a set of all tuples t, such that
Predicate P(t) is true for t
• The curly braces {} are used to indicate that the expression is a set
of tuples
P(t) may have various conditions logically
combined with OR ( ), AND ( ), NOT(¬).
∨ ∧
It also uses quantifiers:
∃ t r (Q(t)) = ”there exists” a tuple in t in
∈
relation r such that predicate Q(t) is true.
∀ t r (Q(t)) = Q(t) is true “for all” tuples in
∈
relation r.
Today: Relational Calculus
For example, let’s say we have a table called
“Employees” with the following attributes: Employee
ID, Name, Salary, Department ID
• To retrieve the names of all employees who earn
more than $50,000 per year, we can use the
following TRC query:
• { t | Employees(t) t.Salary > 50000 }
∧
• In this query, the “Employees(t)” expression specifies
that the tuple variable t represents a row in the
“Employees” table. The “ ” symbol is the logical AND
∧
operator, which is used to combine the condition
“t.Salary > 50000” with the table selection.
• The result of this query will be a set of tuples, where
each tuple contains the Name attribute of an
employee who earns more than $50,000 per year.
Relational Calculus
English example: Find all sailors with a rating above 7
Tuple R.C.:
{S |S Sailors  S.rating > 7}
“From all that is, find me the set of things that are
tuples in the Sailors relation and whose rating field is
greater than 7.”
Domain R.C.:
{<S,N,R,A>| <S,N,R,A> Sailors  R > 7}
“From all that is, find me column values S, N, R, and
A, where S is an integer, N is a string, R is an integer,
A is a floating point number, such that <S, N, R, A> is
a tuple in the Sailors relation and R is greater than 7.”
sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
Tuple Relational Calculus
• Query form: {T | p(T)}
• T is a tuple and p(T) denotes a formula in which tuple
variable T appears.
• Answer:
• set of all tuples T for which the formula p(T)
evaluates to true.
• Formula is recursively defined:
• Atomic formulas get tuples from relations or
compare values
• Formulas built from other formulas using logical
operators.
TRC Formulas
An atomic formula is one of the following:
R  Rel
R.a op S.b
R.a op constant, where
op is one of
A formula can be:
 an atomic formula
 where p and q are formulas
 where variable R is a tuple variable
 where variable R is a tuple variable
  
, , , , ,
  
p p q p q
, ,
)
)
(
( R
p
R

)
)
(
( R
p
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.
• Important restriction
{T | p(T)}
• 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.
Use of  (For every)
• x (P(x)):
only true if P(x) is true for every x in the universe:
e.g. x ((x.color = “Red”)
means everything that exists is red
• Usually we are less grandiose in our assertions:
x ( (x  Boats)  (x.color = “Red”)
•  is a logical implication
a  b means that if a is true, b must be true
a  b is the same as a  b
a  b is the same as a  b
• If a is true, b must
be true!
• If a is true and b is
false, the
expression
evaluates to false.
• If a is not true, we
don’t care about b
• The expression is
always true.
a
T
F
T F
b
T
T T
F
Quantifier Shortcuts
• x ((x  Boats)  (x.color = “Red”))
“For every x in the Boats relation, the color must be Red.”
Can also be written as:
x  Boats(x.color = “Red”)
• x ( (x  Boats)  (x.color = “Red”))
“There exists a tuple x in the Boats relation whose color is
Red.”
Can also be written as:
x  Boats (x.color = “Red”)
Domain Relational Calculus
• In domain relational calculus, filtering variable uses
the domain of attributes. Domain relational calculus
uses the same operators as tuple calculus.
• DRC focuses on expressing queries by specifying the
desired result attributes and the conditions they
must satisfy, without dictating how to retrieve them.
Unlike SQL, it deals with individual values and
conditions within domains (sets of possible values
for an attribute) rather than entire tuples.
• It uses logical connectives (and), (or) and (not).
∧ ∨ ┓
It uses Existential ( ) and Universal Quantifiers ( ) to
∃ ∀
bind the variable. The QBE or Query by example is a
query language related to domain relational
calculus.
Notation:
• { a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where
• a1, a2 are attributes
• P stands for formula built by inner attributes
• { < x1
, x2
, x3
, ..., xn
> | P (x1
, x2
, x3
, ..., xn
) }
• where, < x1, x2, x3, …, xn > represents resulting domains
variables and P (x1, x2, x3, …, xn ) represents the condition
or formula equivalent to the Predicate calculus.
Predicate Calculus Formula:
• Set of all comparison operators
• Set of connectives like and, or, not
• Set of quantifiers
• Example:
• Table-1: Customer
Example
• Query-1: Find the loan number, branch, amount of loans of greater
than or equal to 100 amount.
• { l, b, a | l, b, a loan (a ≥ 100)}
≺ ≻ ≺ ≻ ∈ ∧
• Query-2: Find the loan number for each loan of an amount greater or
equal to 150. {≺l≻ | ∃ b, a (≺l, b, a≻ ∈ loan ∧ (a ≥ 150)}
• Query-3: Find the names of all customers having a loan at the
“Main” branch and find the loan amount .
• { c, a | l ( c, l borrower b ( l, b, a loan (b =
≺ ≻ ∃ ≺ ≻ ∈ ∧ ∃ ≺ ≻ ∈ ∧
“Main”)))}
• Note:
The domain variables those will be in resulting relation must
appear before | within and and all the domain variables
≺ ≻
must appear in which order they are in original relation or table
Selection and Projection
Selection
Find all sailors with rating above 8
sname age
{S |S Sailors  S.rating > 8}
{S | S1 Sailors(S1.rating > 8
 S.sname = S1.sname
 S.age = S1.age)}
S is a tuple variable of 2 fields (i.e. {S} is a projection of
Sailors)
sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
• Projection
Find names and ages of sailors with rating above 8.
S
S1
yuppy 35.0
S1
S1
S1
S rusty 35.0
Joins
Note the use of  to find a tuple in Reserves that `joins
with’ the Sailors tuple under consideration.
{S | SSailors  S.rating > 7 
R(RReserves  R.sid = S.sid
 R.bid = 103)}
Find sailors rated > 7 who’ve reserved boat #103
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
sid bid day
22 101 10/10/96
58 103 11/12/96
S
S
S
R
R
What if there was another tuple {58, 103, 12/13/96} in the Reserves
relation?
Joins (continued)
Notice how the parentheses control the scope of each quantifier’s binding.
{S | SSailors  S.rating > 7 
R(RReserves  R.sid = S.sid
 B(BBoats  B.bid = R.bid
 B.color = ‘red’))}
Find sailors rated > 7 who’ve reserved a red boat
What does this expression compute?
Division
Find all sailors S such that…
A value x in A is disqualified if by attaching a y value from B, we obtain an xy tuple
that is not in A. (e.g: only give me A tuples that have a match in B.
{S | SSailors 
BBoats (RReserves
(S.sid = R.sid
 B.bid = R.bid))}
e.g. Find sailors who’ve reserved all boats:
•Recall the algebra expression A/B…
In calculus, use the  operator:
For all tuples B in
Boats…
There is at least one tuple in
Reserves…
showing that sailor S has reserved B.
Unsafe Queries, Expressive Power
  syntactically correct calculus queries that have an infinite
number of answers! These are unsafe queries.
e.g.,
Solution???? Don’t do that!
Expressive Power (Theorem due to Codd):
Every query that can be expressed in relational algebra can be
expressed as a safe query in DRC / TRC; the converse is also true.
Relational Completeness: Query languages (e.g., SQL) can
express every query that is expressible in relational
algebra/calculus. (actually, SQL is more powerful, as we will
see…)

S| SSailors


















Tuple Relational Calculus (TRC) Vs Domain Relational Calculus (DRC)
Basis of Comparison Tuple Relational Calculus (TRC) Domain Relational Calculus (DRC)
Definition
The Tuple Relational Calculus (TRC) is used to select tuples
from a relation. The tuples with specific range values, tuples
with certain attribute values, and so on can be selected.
The Domain Relational Calculus (DRC) employs a list of
attributes from which to choose based on the condition.
It’s similar to TRC, but instead of selecting entire tuples, it
selects attributes.
Representation of
variables
In TRC, the variables represent the tuples from specified
relations.
In DRC, the variables represent the value drawn from a
specified domain
Tuple/ Domain
A tuple is a single element of relation. In database terms, it is
a row.
A domain is equivalent to column data type and any
constraints on the value of data.
Similarity It reflects traditional pre-relational file structures. It is more similar to logic as a modeling language.
Syntax Notation: {T | P (T)} or {T | Condition (T)} Notation: { a1, a2, a3, …, an | P (a1, a2, a3, …, an)}
Example {T | EMPLOYEE (T) AND T.DEPT_ID = 10} { | < EMPLOYEE > DEPT_ID = 10 }
Filtering This filtering variable uses a tuple of relations. This filtering is done based on the domain of attributes.
Return Value
The predicate expression condition associated with the TRC is
used to test every row using a tuple variable and return those
tuples that met the condition.
DRC takes advantage of domain variables and, based on the
condition set, returns the required attribute or column
that satisfies the criteria of the condition.
Relational Completeness means…
Practice
Query Optimization
and Execution
Relational Operators
Files and Access Methods
Buffer Management
Disk Space Management
DB
Theory
Relational Algebra
Relational Model
Relational Calculus
Now we can study SQL!
Practice
Query Optimization
and Execution
Relational Operators
Files and Access Methods
Buffer Management
Disk Space Management
DB
SQL
Summary
• The relational model has rigorously defined query
languages that are simple and powerful.
• Algebra and safe calculus have same expressive power
• Relational algebra is more operational
• useful as internal representation for query evaluation plans.
… they’ll be baa-aack….
• Relational calculus is more declarative
• users define queries in terms of what they want, not in terms of
how to compute it.
• Almost every query can be expressed several ways
• and that’s what makes query optimization fun!

More Related Content

PPTX
Chapter-7 Relational Calculus
PPT
lecture9Calc.ppt
PPT
Relational Calculus Data Base Managment Systems
PPTX
UNIT-2 Relation algebra&RelationalCalculus.pptx
PPTX
DBMS ppt (1).pptx
PDF
6 - Relational Calculus.pdf
PPTX
Relational Calculus
PPT
Trc final
Chapter-7 Relational Calculus
lecture9Calc.ppt
Relational Calculus Data Base Managment Systems
UNIT-2 Relation algebra&RelationalCalculus.pptx
DBMS ppt (1).pptx
6 - Relational Calculus.pdf
Relational Calculus
Trc final

Similar to Lect 3 Relational Calculus.pptx from mmu (20)

PPTX
Relational Algebra in Database Systems.pptx
PPTX
Relational Model,relational calulus.pptx
PDF
Relational Algebra & Calculus
PPTX
database chapter 6.pptx advanced database
PPT
Relational algebra in database management system
PPT
Introduction to Domain Calculus Notes.ppt
PPT
Presentation on dbms(relational calculus)
PPT
Relational Algebra and Calculus.ppt
PPT
Formal- Relational- Query- Languages.ppt
PPT
Formal-Relational-Query-Languages.ppt for education
PPTX
Relational algebra calculus
PPT
Query Decomposition and data localization
PPT
relational algebra Tuple Relational Calculus - database management system
PPTX
U-2.pptx igxitditdursruzyezrzmyayeTeYrsyrsurzursursursursursrusursruzruRuUeYe...
PPT
[ABDO] Logic As A Database Language
PDF
3.1 tuple relational_calculus
PPT
Admission in india 2015
PPT
Relational calculas
PPT
Ch6 formal relational query languages
PPTX
Relational Algebra.Pptxjejejjdjdh jsjsjd
Relational Algebra in Database Systems.pptx
Relational Model,relational calulus.pptx
Relational Algebra & Calculus
database chapter 6.pptx advanced database
Relational algebra in database management system
Introduction to Domain Calculus Notes.ppt
Presentation on dbms(relational calculus)
Relational Algebra and Calculus.ppt
Formal- Relational- Query- Languages.ppt
Formal-Relational-Query-Languages.ppt for education
Relational algebra calculus
Query Decomposition and data localization
relational algebra Tuple Relational Calculus - database management system
U-2.pptx igxitditdursruzyezrzmyayeTeYrsyrsurzursursursursursrusursruzruRuUeYe...
[ABDO] Logic As A Database Language
3.1 tuple relational_calculus
Admission in india 2015
Relational calculas
Ch6 formal relational query languages
Relational Algebra.Pptxjejejjdjdh jsjsjd
Ad

Recently uploaded (20)

PPTX
ai tools demonstartion for schools and inter college
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
L1 - Introduction to python Backend.pptx
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Nekopoi APK 2025 free lastest update
PDF
AI in Product Development-omnex systems
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
medical staffing services at VALiNTRY
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Understanding Forklifts - TECH EHS Solution
ai tools demonstartion for schools and inter college
Upgrade and Innovation Strategies for SAP ERP Customers
Which alternative to Crystal Reports is best for small or large businesses.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
L1 - Introduction to python Backend.pptx
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Nekopoi APK 2025 free lastest update
AI in Product Development-omnex systems
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Odoo Companies in India – Driving Business Transformation.pdf
PTS Company Brochure 2025 (1).pdf.......
2025 Textile ERP Trends: SAP, Odoo & Oracle
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
medical staffing services at VALiNTRY
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Operating system designcfffgfgggggggvggggggggg
How to Choose the Right IT Partner for Your Business in Malaysia
Understanding Forklifts - TECH EHS Solution
Ad

Lect 3 Relational Calculus.pptx from mmu

  • 1. Lect III B Relational Calculus What will be covered • Introduction • Why is it called Relational calculus • Common Notations Used in Relational Calculus • Types of Relational Calculus • Examples • Comparison between TRC & DRC
  • 2. Introduction: Relational Calculus • Relational Calculus in DBMS is just another way of formulating queries. Relational calculus is a non- procedural query language that tells the system what data to be retrieved but doesn’t tell how to retrieve it. The relational calculus tells what to do but never explains how to do. Most commercial relational languages are based on aspects of relational calculus including SQL-QBE and QUEL.
  • 3. Why is it called Relational Calculus? • It is called Relational Calculus because it is used to manipulate and describe relational data in DBMS using a mathematical and logical approach. The “relational” part refers to representing and organizing the data in a tabular form with the help of relations or tables. The “calculus” part signifies a system of calculation and reasoning. • Relational Calculus is based on predicate calculus, a truth-valued function with arguments. When the values of the arguments are substituted in the predicate calculus, the resulting expression is called a proposition which can be either true or false. • Relational calculus is a tailored version of a subset of Predicate Calculus, which helps communicate with the relational database.
  • 4. Common Notations Used in Relational Calculus • Comparison operators =, <>, <, >, • Logical connectives ∧ - represents AND. ∨ - represents OR. ¬ - represents NOT. ∈ - represents Belongs to. =>- implies • Quantifiers (Universal Quantifier). • ∀ - (for all): All tuples satisfy the condition. • represents that in a set of tuples, all of them satisfy the condition. • ∃ - (exists): There exists at least one tuple satisfying the condition. represents that in a set of tuples, there is at least
  • 5. Set Operations: ∪: Union ∩: Intersection -: Difference Additional Symbols: π: Projection (selecting specific attributes) σ: Selection (filtering tuples based on conditions) ρ: Renaming (changing attribute names)
  • 7. TRC vs DRC •TRC: Variables range over (i.e., get bound to) tuples. •DRC: Variables range over domain elements (= attribute values) •Both TRC and DRC are subsets of first-order logic We use some short-hand notation to simplify formulas •Expressions in the calculus are called formulas •Answer tuple = assignment of constants to variables that make the formula evaluate to true
  • 8. Tuple Relational Calculus (TRC) • TRC a non-procedural query language used in relational database management systems (RDBMS) to retrieve data from tables. TRC is based on the concept of tuples, which are ordered sets of attribute values that represent a single row or record in a database table. • It is a non-procedural query language with which we find tuples that hold true for a given condition. These given conditions are called predicates. Tuple Relational Calculus describes the desired information without providing specific information for obtaining that information, i.e., specifies ‘what’ but not ‘how.’ • It goes to each table row and checks whether the predicate condition is satisfied (true) or not (false). It returns the tuple(s) which hold true for the predicate condition.
  • 9. • TRC is a declarative language, meaning that it specifies what data is required from the database, rather than how to retrieve it. TRC queries are expressed as logical formulas that describe the desired tuples. Syntax: The basic syntax of TRC is as follows: {t | P(t)} • where t is a tuple variable or resulting tuples • P(t) is a logical formula that describes the conditions that the tuples in the result must satisfy. • P(t) = known as Predicate and these are the conditions that are used to fetch t. Thus, it generates a set of all tuples t, such that Predicate P(t) is true for t • The curly braces {} are used to indicate that the expression is a set of tuples
  • 10. P(t) may have various conditions logically combined with OR ( ), AND ( ), NOT(¬). ∨ ∧ It also uses quantifiers: ∃ t r (Q(t)) = ”there exists” a tuple in t in ∈ relation r such that predicate Q(t) is true. ∀ t r (Q(t)) = Q(t) is true “for all” tuples in ∈ relation r.
  • 11. Today: Relational Calculus For example, let’s say we have a table called “Employees” with the following attributes: Employee ID, Name, Salary, Department ID • To retrieve the names of all employees who earn more than $50,000 per year, we can use the following TRC query: • { t | Employees(t) t.Salary > 50000 } ∧ • In this query, the “Employees(t)” expression specifies that the tuple variable t represents a row in the “Employees” table. The “ ” symbol is the logical AND ∧ operator, which is used to combine the condition “t.Salary > 50000” with the table selection. • The result of this query will be a set of tuples, where each tuple contains the Name attribute of an employee who earns more than $50,000 per year.
  • 12. Relational Calculus English example: Find all sailors with a rating above 7 Tuple R.C.: {S |S Sailors  S.rating > 7} “From all that is, find me the set of things that are tuples in the Sailors relation and whose rating field is greater than 7.” Domain R.C.: {<S,N,R,A>| <S,N,R,A> Sailors  R > 7} “From all that is, find me column values S, N, R, and A, where S is an integer, N is a string, R is an integer, A is a floating point number, such that <S, N, R, A> is a tuple in the Sailors relation and R is greater than 7.” sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0
  • 13. Tuple Relational Calculus • Query form: {T | p(T)} • T is a tuple and p(T) denotes a formula in which tuple variable T appears. • Answer: • set of all tuples T for which the formula p(T) evaluates to true. • Formula is recursively defined: • Atomic formulas get tuples from relations or compare values • Formulas built from other formulas using logical operators.
  • 14. TRC Formulas An atomic formula is one of the following: R  Rel R.a op S.b R.a op constant, where op is one of A formula can be:  an atomic formula  where p and q are formulas  where variable R is a tuple variable  where variable R is a tuple variable    , , , , ,    p p q p q , , ) ) ( ( R p R  ) ) ( ( R p R 
  • 15. 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. • Important restriction {T | p(T)} • 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.
  • 16. Use of  (For every) • x (P(x)): only true if P(x) is true for every x in the universe: e.g. x ((x.color = “Red”) means everything that exists is red • Usually we are less grandiose in our assertions: x ( (x  Boats)  (x.color = “Red”) •  is a logical implication a  b means that if a is true, b must be true a  b is the same as a  b
  • 17. a  b is the same as a  b • If a is true, b must be true! • If a is true and b is false, the expression evaluates to false. • If a is not true, we don’t care about b • The expression is always true. a T F T F b T T T F
  • 18. Quantifier Shortcuts • x ((x  Boats)  (x.color = “Red”)) “For every x in the Boats relation, the color must be Red.” Can also be written as: x  Boats(x.color = “Red”) • x ( (x  Boats)  (x.color = “Red”)) “There exists a tuple x in the Boats relation whose color is Red.” Can also be written as: x  Boats (x.color = “Red”)
  • 19. Domain Relational Calculus • In domain relational calculus, filtering variable uses the domain of attributes. Domain relational calculus uses the same operators as tuple calculus. • DRC focuses on expressing queries by specifying the desired result attributes and the conditions they must satisfy, without dictating how to retrieve them. Unlike SQL, it deals with individual values and conditions within domains (sets of possible values for an attribute) rather than entire tuples. • It uses logical connectives (and), (or) and (not). ∧ ∨ ┓ It uses Existential ( ) and Universal Quantifiers ( ) to ∃ ∀ bind the variable. The QBE or Query by example is a query language related to domain relational calculus.
  • 20. Notation: • { a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)} Where • a1, a2 are attributes • P stands for formula built by inner attributes • { < x1 , x2 , x3 , ..., xn > | P (x1 , x2 , x3 , ..., xn ) } • where, < x1, x2, x3, …, xn > represents resulting domains variables and P (x1, x2, x3, …, xn ) represents the condition or formula equivalent to the Predicate calculus.
  • 21. Predicate Calculus Formula: • Set of all comparison operators • Set of connectives like and, or, not • Set of quantifiers • Example: • Table-1: Customer
  • 22. Example • Query-1: Find the loan number, branch, amount of loans of greater than or equal to 100 amount. • { l, b, a | l, b, a loan (a ≥ 100)} ≺ ≻ ≺ ≻ ∈ ∧ • Query-2: Find the loan number for each loan of an amount greater or equal to 150. {≺l≻ | ∃ b, a (≺l, b, a≻ ∈ loan ∧ (a ≥ 150)}
  • 23. • Query-3: Find the names of all customers having a loan at the “Main” branch and find the loan amount . • { c, a | l ( c, l borrower b ( l, b, a loan (b = ≺ ≻ ∃ ≺ ≻ ∈ ∧ ∃ ≺ ≻ ∈ ∧ “Main”)))} • Note: The domain variables those will be in resulting relation must appear before | within and and all the domain variables ≺ ≻ must appear in which order they are in original relation or table
  • 24. Selection and Projection Selection Find all sailors with rating above 8 sname age {S |S Sailors  S.rating > 8} {S | S1 Sailors(S1.rating > 8  S.sname = S1.sname  S.age = S1.age)} S is a tuple variable of 2 fields (i.e. {S} is a projection of Sailors) sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 • Projection Find names and ages of sailors with rating above 8. S S1 yuppy 35.0 S1 S1 S1 S rusty 35.0
  • 25. Joins Note the use of  to find a tuple in Reserves that `joins with’ the Sailors tuple under consideration. {S | SSailors  S.rating > 7  R(RReserves  R.sid = S.sid  R.bid = 103)} Find sailors rated > 7 who’ve reserved boat #103 sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 sid bid day 22 101 10/10/96 58 103 11/12/96 S S S R R What if there was another tuple {58, 103, 12/13/96} in the Reserves relation?
  • 26. Joins (continued) Notice how the parentheses control the scope of each quantifier’s binding. {S | SSailors  S.rating > 7  R(RReserves  R.sid = S.sid  B(BBoats  B.bid = R.bid  B.color = ‘red’))} Find sailors rated > 7 who’ve reserved a red boat What does this expression compute?
  • 27. Division Find all sailors S such that… A value x in A is disqualified if by attaching a y value from B, we obtain an xy tuple that is not in A. (e.g: only give me A tuples that have a match in B. {S | SSailors  BBoats (RReserves (S.sid = R.sid  B.bid = R.bid))} e.g. Find sailors who’ve reserved all boats: •Recall the algebra expression A/B… In calculus, use the  operator: For all tuples B in Boats… There is at least one tuple in Reserves… showing that sailor S has reserved B.
  • 28. Unsafe Queries, Expressive Power   syntactically correct calculus queries that have an infinite number of answers! These are unsafe queries. e.g., Solution???? Don’t do that! Expressive Power (Theorem due to Codd): Every query that can be expressed in relational algebra can be expressed as a safe query in DRC / TRC; the converse is also true. Relational Completeness: Query languages (e.g., SQL) can express every query that is expressible in relational algebra/calculus. (actually, SQL is more powerful, as we will see…)  S| SSailors                  
  • 29. Tuple Relational Calculus (TRC) Vs Domain Relational Calculus (DRC) Basis of Comparison Tuple Relational Calculus (TRC) Domain Relational Calculus (DRC) Definition The Tuple Relational Calculus (TRC) is used to select tuples from a relation. The tuples with specific range values, tuples with certain attribute values, and so on can be selected. The Domain Relational Calculus (DRC) employs a list of attributes from which to choose based on the condition. It’s similar to TRC, but instead of selecting entire tuples, it selects attributes. Representation of variables In TRC, the variables represent the tuples from specified relations. In DRC, the variables represent the value drawn from a specified domain Tuple/ Domain A tuple is a single element of relation. In database terms, it is a row. A domain is equivalent to column data type and any constraints on the value of data. Similarity It reflects traditional pre-relational file structures. It is more similar to logic as a modeling language. Syntax Notation: {T | P (T)} or {T | Condition (T)} Notation: { a1, a2, a3, …, an | P (a1, a2, a3, …, an)} Example {T | EMPLOYEE (T) AND T.DEPT_ID = 10} { | < EMPLOYEE > DEPT_ID = 10 } Filtering This filtering variable uses a tuple of relations. This filtering is done based on the domain of attributes. Return Value The predicate expression condition associated with the TRC is used to test every row using a tuple variable and return those tuples that met the condition. DRC takes advantage of domain variables and, based on the condition set, returns the required attribute or column that satisfies the criteria of the condition.
  • 30. Relational Completeness means… Practice Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB Theory Relational Algebra Relational Model Relational Calculus
  • 31. Now we can study SQL! Practice Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB SQL
  • 32. Summary • The relational model has rigorously defined query languages that are simple and powerful. • Algebra and safe calculus have same expressive power • Relational algebra is more operational • useful as internal representation for query evaluation plans. … they’ll be baa-aack…. • Relational calculus is more declarative • users define queries in terms of what they want, not in terms of how to compute it. • Almost every query can be expressed several ways • and that’s what makes query optimization fun!