SlideShare a Scribd company logo
1 of 27
Department of Information Technology 1Data base Technologies (ITB4201)
Introduction to Relational Calculus
Dr. C.V. Suresh Babu
Professor
Department of IT
Hindustan Institute of Science & Technology
Department of Information Technology 2Data base Technologies (ITB4201)
Discussion Topics
• DBMS Architecture
• Relational Algebra Review
• Relational calculus
• Relational calculus building blocks
• Tuple relational calculus
• Tuple relational calculus Formulas
• Quiz
Department of Information Technology 3Data base Technologies (ITB4201)
DBMS Architecture
How does a SQL engine work ?
• SQL query  relational
algebra plan
• Relational algebra plan 
Optimized plan
• Execute each operator of the
plan
Query Optimization
and Execution
Relational Operators
Files and Access Methods
Buffer Management
Disk Space Management
DB
PracticeTheory
Relational Algebra
Relational Model
Relational Calculus
Department of Information Technology 4Data base Technologies (ITB4201)
Where are we going next?
Query Optimization
and Execution
Relational Operators
Files and Access Methods
Buffer Management
Disk Space Management
DB
Practice
SQL
On Deck:
Practical
ways of
evaluating
SQL
Department of Information Technology 5Data base Technologies (ITB4201)
Review – Why do we need Query Languages anyway?
• Two key advantages
– Less work for user asking query
– More opportunities for optimization
• Relational Algebra
– Theoretical foundation for SQL
– Higher level than programming language
• but still must specify steps to get desired result
• Relational Calculus
– Formal foundation for Query-by-Example
– A first-order logic description of desired result
– Only specify desired result, not how to get it
Department of Information Technology 6Data base Technologies (ITB4201)
Additional operations:
•Intersection ()
•Join ( )
•Division ( / )
Relational Algebra Review
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
bid bname color
101 Interlake Blue
102 Interlake Red
103 Clipper Green
104 Marine Red
sid bid day
22 101 10/10/96
58 103 11/12/96
Reserves Sailors Boats
Basic operations:
•Selection ( σ )
•Projection ( π )
•Cross-product (  )
•Set-difference ( — )
•Union (  )
:tuples in both relations.
:like  but only keep tuples where common fields are equal.
:tuples from relation 1 with matches in relation 2
: gives a subset of rows.
: deletes unwanted columns.
: combine two relations.
: tuples in relation 1, but not 2
: tuples in relation 1 and 2.
Query Optimization
and Execution
Relational Operators
Files and Access Methods
Buffer Management
Disk Space Management
DB
Prediction: These
relational operators are
going to look hauntingly
familiar when we get to
them…!
Department of Information Technology 7Data base Technologies (ITB4201)
Additional operations:
•Intersection ()
•Join ( )
•Division ( / )
Relational Algebra Review
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
bid bname color
101 Interlake Blue
102 Interlake Red
103 Clipper Green
104 Marine Red
sid bid day
22 101 10/10/96
58 103 11/12/96
Reserves Sailors Boats
Basic operations:
•Selection ( σ )
•Projection ( π )
•Cross-product (  )
•Set-difference ( — )
•Union (  )
Find names of sailors who’ve reserved a green boat
σ( color=‘Green’Boats)( Sailors)π( sname )( Reserves)
Department of Information Technology 8Data base Technologies (ITB4201)
Relational Algebra Review
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
bid bname color
101 Interlake Blue
102 Interlake Red
103 Clipper Green
104 Marine Red
sid bid day
22 101 10/10/96
58 103 11/12/96
Reserves Sailors Boats
Find names of sailors who’ve reserved a green boat
Given the previous algebra, a query optimizer would replace it with this!
σ( color=‘Green’Boats)
( Sailors)
π( sname )
( Reserves)
π( bid )
π( sid )
Or better yet:
Department of Information Technology 9Data base Technologies (ITB4201)
Relational Calculus
• High-level, first-order logic description
– A formal definition of what you want from the database
• e.g. English:
“Find all sailors with a rating above 7”
In Calculus:
{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.”
• Two flavors:
– Tuple relational calculus (TRC) (Like SQL)
– Domain relational calculus (DRC) (Like QBE)
Department of Information Technology 10Data base Technologies (ITB4201)
Relational Calculus Building Blocks
• Variables
TRC: Variables are bound to tuples.
DRC: Variables are bound to domain elements (= column values)
• Constants
7, “Foo”, 3.14159, etc.
• Comparison operators
=, <>, <, >, etc.
• Logical connectives
 - not
 – and
 - or
- implies
 - is a member of
• Quantifiers
X(p(X)): For every X, p(X) must be true
X(p(X)): There exists at least one X such that p(X) is true
Department of Information Technology 11Data base Technologies (ITB4201)
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
Department of Information Technology 12Data base Technologies (ITB4201)
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.
Department of Information Technology 13Data base Technologies (ITB4201)
• 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
TRC Formulas
     , , , , ,
  p p q p q, ,
))(( RpR
))(( RpR
Department of Information Technology 14Data base Technologies (ITB4201)
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.
Department of Information Technology 15Data base Technologies (ITB4201)
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
Department of Information Technology 16Data base Technologies (ITB4201)
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
Department of Information Technology 17Data base Technologies (ITB4201)
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”)
Department of Information Technology 18Data base Technologies (ITB4201)
Selection and Projection
• Selection
Find all sailors with rating above 8
{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
sname age
• Projection
Find names and ages of sailors with rating above 8.
S
S1
yuppy 35.0
S1
S1
S1
S rusty 35.0
Department of Information Technology 19Data base Technologies (ITB4201)
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)}
Joins
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?
Department of Information Technology 20Data base Technologies (ITB4201)
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?
Department of Information Technology 21Data base Technologies (ITB4201)
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.
Department of Information Technology 22Data base Technologies (ITB4201)
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

















Department of Information Technology 23Data base Technologies (ITB4201)
Relational Completeness means…
Query Optimization
and Execution
Relational Operators
Files and Access Methods
Buffer Management
Disk Space Management
DB
PracticeTheory
Relational Algebra
Relational Model
Relational Calculus
Department of Information Technology 24Data base Technologies (ITB4201)
Now we can study SQL!
Query Optimization
and Execution
Relational Operators
Files and Access Methods
Buffer Management
Disk Space Management
DB
Practice
SQL
Department of Information Technology 25Data base Technologies (ITB4201)
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.
• 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!
Department of Information Technology 26Data base Technologies (ITB4201)
Test Yourself
1. Because of the calculus expression, the relational calculus in considered as
a) procedural language
b) non procedural language
c) structural language
d) functional language
2. Relational calculus is:
i. equivalent to relational algebra in its capabilities.
Ii. It is stronger than relational algebra
Iii. It is weaker than relational algebra.
Iv. It is based on predicate calculus of formal logic
a) (i) and (iv) are true
b) (ii) and (iv) are true
c) only (iii) is true
d) (iii) and (iv) are true
3. Which of the following symbol is used in the place of except?
a) ^ b) V c) ¬ d) ~
4. Which of the following is the comparison operator in tuple relational calculus
1.⇒ b) = c) ε d) All of the mentioned
5. In tuple relational calculus P1 → P2 is equivalent to
a)¬P1 ∨ P2
b)¬P1 ∨ P2
c)P1 ∧ P2
d)P1 ∧ ¬P2
Department of Information Technology 27Data base Technologies (ITB4201)
Answers
1. Because of the calculus expression, the relational calculus in considered as
a) procedural language
b) non procedural language
c) structural language
d) functional language
2. Relational calculus is:
i. equivalent to relational algebra in its capabilities.
Ii. It is stronger than relational algebra
Iii. It is weaker than relational algebra.
Iv. It is based on predicate calculus of formal logic
a) (i) and (iv) are true
b) (ii) and (iv) are true
c) only (iii) is true
d) (iii) and (iv) are true
3. Which of the following symbol is used in the place of except?
a) ^ b) V c) ¬ d) ~
4. Which of the following is the comparison operator in tuple relational calculus
1.⇒ b) = c) ε d) All of the mentioned
5. In tuple relational calculus P1 → P2 is equivalent to
a)¬P1 ∨ P2
b)¬P1 ∨ P2
c)P1 ∧ P2
d)P1 ∧ ¬P2

More Related Content

What's hot

Database Normalization
Database NormalizationDatabase Normalization
Database NormalizationArun Sharma
 
Functional dependency and normalization
Functional dependency and normalizationFunctional dependency and normalization
Functional dependency and normalizationUniversity of Potsdam
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbmsshekhar1991
 
Relational calculas
Relational calculasRelational calculas
Relational calculasanuj24
 
Functional dependencies and normalization
Functional dependencies and normalizationFunctional dependencies and normalization
Functional dependencies and normalizationdaxesh chauhan
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++Neeru Mittal
 
Structure of dbms
Structure of dbmsStructure of dbms
Structure of dbmsMegha yadav
 
Relational algebra (basics)
Relational algebra (basics)Relational algebra (basics)
Relational algebra (basics)usama nizam
 
6. Integrity and Security in DBMS
6. Integrity and Security in DBMS6. Integrity and Security in DBMS
6. Integrity and Security in DBMSkoolkampus
 
Fifth normal form
Fifth normal formFifth normal form
Fifth normal formAthi Sethu
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational modelChirag vasava
 
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
FUNCTION DEPENDENCY  AND TYPES & EXAMPLEFUNCTION DEPENDENCY  AND TYPES & EXAMPLE
FUNCTION DEPENDENCY AND TYPES & EXAMPLEVraj Patel
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals INick Buytaert
 
Procedural vs. object oriented programming
Procedural vs. object oriented programmingProcedural vs. object oriented programming
Procedural vs. object oriented programmingHaris Bin Zahid
 
boolean algrebra and logic gates in short
boolean algrebra and logic gates in shortboolean algrebra and logic gates in short
boolean algrebra and logic gates in shortRojin Khadka
 

What's hot (20)

Database Normalization
Database NormalizationDatabase Normalization
Database Normalization
 
Functional dependency and normalization
Functional dependency and normalizationFunctional dependency and normalization
Functional dependency and normalization
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
Relational calculas
Relational calculasRelational calculas
Relational calculas
 
Functional dependencies and normalization
Functional dependencies and normalizationFunctional dependencies and normalization
Functional dependencies and normalization
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Structure of dbms
Structure of dbmsStructure of dbms
Structure of dbms
 
Relational algebra (basics)
Relational algebra (basics)Relational algebra (basics)
Relational algebra (basics)
 
6. Integrity and Security in DBMS
6. Integrity and Security in DBMS6. Integrity and Security in DBMS
6. Integrity and Security in DBMS
 
Fifth normal form
Fifth normal formFifth normal form
Fifth normal form
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
 
Normal forms
Normal formsNormal forms
Normal forms
 
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
FUNCTION DEPENDENCY  AND TYPES & EXAMPLEFUNCTION DEPENDENCY  AND TYPES & EXAMPLE
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
 
relational algebra
relational algebrarelational algebra
relational algebra
 
Lossless decomposition
Lossless decompositionLossless decomposition
Lossless decomposition
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 
joins in database
 joins in database joins in database
joins in database
 
Procedural vs. object oriented programming
Procedural vs. object oriented programmingProcedural vs. object oriented programming
Procedural vs. object oriented programming
 
boolean algrebra and logic gates in short
boolean algrebra and logic gates in shortboolean algrebra and logic gates in short
boolean algrebra and logic gates in short
 

Similar to Relational Calculus

Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015Edhole.com
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational AlgebraAmin Omi
 
Relational Algebra and Calculus.ppt
Relational Algebra and Calculus.pptRelational Algebra and Calculus.ppt
Relational Algebra and Calculus.pptAnkush138
 
Query Optimization - Brandon Latronica
Query Optimization - Brandon LatronicaQuery Optimization - Brandon Latronica
Query Optimization - Brandon Latronica"FENG "GEORGE"" YU
 
Relational Database and Relational Algebra
Relational Database and Relational AlgebraRelational Database and Relational Algebra
Relational Database and Relational AlgebraPyingkodi Maran
 
Programming in python
Programming in pythonProgramming in python
Programming in pythonIvan Rojas
 
Data Analytics with R and SQL Server
Data Analytics with R and SQL ServerData Analytics with R and SQL Server
Data Analytics with R and SQL ServerStéphane Fréchette
 
Knowledge Discovery Query Language (KDQL)
Knowledge Discovery Query Language (KDQL)Knowledge Discovery Query Language (KDQL)
Knowledge Discovery Query Language (KDQL)Zakaria Zubi
 
Logic
LogicLogic
LogicHamxi
 
CS 542 -- Query Execution
CS 542 -- Query ExecutionCS 542 -- Query Execution
CS 542 -- Query ExecutionJ Singh
 
lecture 41 cost optimization.pptx
lecture 41 cost optimization.pptxlecture 41 cost optimization.pptx
lecture 41 cost optimization.pptxSundaramyadav3
 
Intro to Data warehousing lecture 11
Intro to Data warehousing   lecture 11Intro to Data warehousing   lecture 11
Intro to Data warehousing lecture 11AnwarrChaudary
 
Intro to Data warehousing lecture 14
Intro to Data warehousing   lecture 14Intro to Data warehousing   lecture 14
Intro to Data warehousing lecture 14AnwarrChaudary
 
Intro to Data warehousing lecture 19
Intro to Data warehousing   lecture 19Intro to Data warehousing   lecture 19
Intro to Data warehousing lecture 19AnwarrChaudary
 

Similar to Relational Calculus (20)

Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
Data structure-question-bank
Data structure-question-bankData structure-question-bank
Data structure-question-bank
 
Relational Algebra and Calculus.ppt
Relational Algebra and Calculus.pptRelational Algebra and Calculus.ppt
Relational Algebra and Calculus.ppt
 
Query Optimization - Brandon Latronica
Query Optimization - Brandon LatronicaQuery Optimization - Brandon Latronica
Query Optimization - Brandon Latronica
 
Relational Database and Relational Algebra
Relational Database and Relational AlgebraRelational Database and Relational Algebra
Relational Database and Relational Algebra
 
Programming in python
Programming in pythonProgramming in python
Programming in python
 
R Language Introduction
R Language IntroductionR Language Introduction
R Language Introduction
 
8 query
8 query8 query
8 query
 
Data Analytics with R and SQL Server
Data Analytics with R and SQL ServerData Analytics with R and SQL Server
Data Analytics with R and SQL Server
 
Knowledge Discovery Query Language (KDQL)
Knowledge Discovery Query Language (KDQL)Knowledge Discovery Query Language (KDQL)
Knowledge Discovery Query Language (KDQL)
 
Logic
LogicLogic
Logic
 
Algebra
AlgebraAlgebra
Algebra
 
Realtime Analytics
Realtime AnalyticsRealtime Analytics
Realtime Analytics
 
CS 542 -- Query Execution
CS 542 -- Query ExecutionCS 542 -- Query Execution
CS 542 -- Query Execution
 
lecture 41 cost optimization.pptx
lecture 41 cost optimization.pptxlecture 41 cost optimization.pptx
lecture 41 cost optimization.pptx
 
Intro to Data warehousing lecture 11
Intro to Data warehousing   lecture 11Intro to Data warehousing   lecture 11
Intro to Data warehousing lecture 11
 
Intro to Data warehousing lecture 14
Intro to Data warehousing   lecture 14Intro to Data warehousing   lecture 14
Intro to Data warehousing lecture 14
 
Intro to Data warehousing lecture 19
Intro to Data warehousing   lecture 19Intro to Data warehousing   lecture 19
Intro to Data warehousing lecture 19
 

More from Dr. C.V. Suresh Babu (20)

Data analytics with R
Data analytics with RData analytics with R
Data analytics with R
 
Association rules
Association rulesAssociation rules
Association rules
 
Clustering
ClusteringClustering
Clustering
 
Classification
ClassificationClassification
Classification
 
Blue property assumptions.
Blue property assumptions.Blue property assumptions.
Blue property assumptions.
 
Introduction to regression
Introduction to regressionIntroduction to regression
Introduction to regression
 
DART
DARTDART
DART
 
Mycin
MycinMycin
Mycin
 
Expert systems
Expert systemsExpert systems
Expert systems
 
Dempster shafer theory
Dempster shafer theoryDempster shafer theory
Dempster shafer theory
 
Bayes network
Bayes networkBayes network
Bayes network
 
Bayes' theorem
Bayes' theoremBayes' theorem
Bayes' theorem
 
Knowledge based agents
Knowledge based agentsKnowledge based agents
Knowledge based agents
 
Rule based system
Rule based systemRule based system
Rule based system
 
Formal Logic in AI
Formal Logic in AIFormal Logic in AI
Formal Logic in AI
 
Production based system
Production based systemProduction based system
Production based system
 
Game playing in AI
Game playing in AIGame playing in AI
Game playing in AI
 
Diagnosis test of diabetics and hypertension by AI
Diagnosis test of diabetics and hypertension by AIDiagnosis test of diabetics and hypertension by AI
Diagnosis test of diabetics and hypertension by AI
 
A study on “impact of artificial intelligence in covid19 diagnosis”
A study on “impact of artificial intelligence in covid19 diagnosis”A study on “impact of artificial intelligence in covid19 diagnosis”
A study on “impact of artificial intelligence in covid19 diagnosis”
 
A study on “impact of artificial intelligence in covid19 diagnosis”
A study on “impact of artificial intelligence in covid19 diagnosis”A study on “impact of artificial intelligence in covid19 diagnosis”
A study on “impact of artificial intelligence in covid19 diagnosis”
 

Recently uploaded

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 

Recently uploaded (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 

Relational Calculus

  • 1. Department of Information Technology 1Data base Technologies (ITB4201) Introduction to Relational Calculus Dr. C.V. Suresh Babu Professor Department of IT Hindustan Institute of Science & Technology
  • 2. Department of Information Technology 2Data base Technologies (ITB4201) Discussion Topics • DBMS Architecture • Relational Algebra Review • Relational calculus • Relational calculus building blocks • Tuple relational calculus • Tuple relational calculus Formulas • Quiz
  • 3. Department of Information Technology 3Data base Technologies (ITB4201) DBMS Architecture How does a SQL engine work ? • SQL query  relational algebra plan • Relational algebra plan  Optimized plan • Execute each operator of the plan Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB PracticeTheory Relational Algebra Relational Model Relational Calculus
  • 4. Department of Information Technology 4Data base Technologies (ITB4201) Where are we going next? Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB Practice SQL On Deck: Practical ways of evaluating SQL
  • 5. Department of Information Technology 5Data base Technologies (ITB4201) Review – Why do we need Query Languages anyway? • Two key advantages – Less work for user asking query – More opportunities for optimization • Relational Algebra – Theoretical foundation for SQL – Higher level than programming language • but still must specify steps to get desired result • Relational Calculus – Formal foundation for Query-by-Example – A first-order logic description of desired result – Only specify desired result, not how to get it
  • 6. Department of Information Technology 6Data base Technologies (ITB4201) Additional operations: •Intersection () •Join ( ) •Division ( / ) Relational Algebra Review sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 bid bname color 101 Interlake Blue 102 Interlake Red 103 Clipper Green 104 Marine Red sid bid day 22 101 10/10/96 58 103 11/12/96 Reserves Sailors Boats Basic operations: •Selection ( σ ) •Projection ( π ) •Cross-product (  ) •Set-difference ( — ) •Union (  ) :tuples in both relations. :like  but only keep tuples where common fields are equal. :tuples from relation 1 with matches in relation 2 : gives a subset of rows. : deletes unwanted columns. : combine two relations. : tuples in relation 1, but not 2 : tuples in relation 1 and 2. Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB Prediction: These relational operators are going to look hauntingly familiar when we get to them…!
  • 7. Department of Information Technology 7Data base Technologies (ITB4201) Additional operations: •Intersection () •Join ( ) •Division ( / ) Relational Algebra Review sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 bid bname color 101 Interlake Blue 102 Interlake Red 103 Clipper Green 104 Marine Red sid bid day 22 101 10/10/96 58 103 11/12/96 Reserves Sailors Boats Basic operations: •Selection ( σ ) •Projection ( π ) •Cross-product (  ) •Set-difference ( — ) •Union (  ) Find names of sailors who’ve reserved a green boat σ( color=‘Green’Boats)( Sailors)π( sname )( Reserves)
  • 8. Department of Information Technology 8Data base Technologies (ITB4201) Relational Algebra Review sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 bid bname color 101 Interlake Blue 102 Interlake Red 103 Clipper Green 104 Marine Red sid bid day 22 101 10/10/96 58 103 11/12/96 Reserves Sailors Boats Find names of sailors who’ve reserved a green boat Given the previous algebra, a query optimizer would replace it with this! σ( color=‘Green’Boats) ( Sailors) π( sname ) ( Reserves) π( bid ) π( sid ) Or better yet:
  • 9. Department of Information Technology 9Data base Technologies (ITB4201) Relational Calculus • High-level, first-order logic description – A formal definition of what you want from the database • e.g. English: “Find all sailors with a rating above 7” In Calculus: {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.” • Two flavors: – Tuple relational calculus (TRC) (Like SQL) – Domain relational calculus (DRC) (Like QBE)
  • 10. Department of Information Technology 10Data base Technologies (ITB4201) Relational Calculus Building Blocks • Variables TRC: Variables are bound to tuples. DRC: Variables are bound to domain elements (= column values) • Constants 7, “Foo”, 3.14159, etc. • Comparison operators =, <>, <, >, etc. • Logical connectives  - not  – and  - or - implies  - is a member of • Quantifiers X(p(X)): For every X, p(X) must be true X(p(X)): There exists at least one X such that p(X) is true
  • 11. Department of Information Technology 11Data base Technologies (ITB4201) 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
  • 12. Department of Information Technology 12Data base Technologies (ITB4201) 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.
  • 13. Department of Information Technology 13Data base Technologies (ITB4201) • 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 TRC Formulas      , , , , ,   p p q p q, , ))(( RpR ))(( RpR
  • 14. Department of Information Technology 14Data base Technologies (ITB4201) 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.
  • 15. Department of Information Technology 15Data base Technologies (ITB4201) 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
  • 16. Department of Information Technology 16Data base Technologies (ITB4201) 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
  • 17. Department of Information Technology 17Data base Technologies (ITB4201) 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”)
  • 18. Department of Information Technology 18Data base Technologies (ITB4201) Selection and Projection • Selection Find all sailors with rating above 8 {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 sname age • Projection Find names and ages of sailors with rating above 8. S S1 yuppy 35.0 S1 S1 S1 S rusty 35.0
  • 19. Department of Information Technology 19Data base Technologies (ITB4201) 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)} Joins 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?
  • 20. Department of Information Technology 20Data base Technologies (ITB4201) 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?
  • 21. Department of Information Technology 21Data base Technologies (ITB4201) 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.
  • 22. Department of Information Technology 22Data base Technologies (ITB4201) 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                 
  • 23. Department of Information Technology 23Data base Technologies (ITB4201) Relational Completeness means… Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB PracticeTheory Relational Algebra Relational Model Relational Calculus
  • 24. Department of Information Technology 24Data base Technologies (ITB4201) Now we can study SQL! Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB Practice SQL
  • 25. Department of Information Technology 25Data base Technologies (ITB4201) 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. • 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!
  • 26. Department of Information Technology 26Data base Technologies (ITB4201) Test Yourself 1. Because of the calculus expression, the relational calculus in considered as a) procedural language b) non procedural language c) structural language d) functional language 2. Relational calculus is: i. equivalent to relational algebra in its capabilities. Ii. It is stronger than relational algebra Iii. It is weaker than relational algebra. Iv. It is based on predicate calculus of formal logic a) (i) and (iv) are true b) (ii) and (iv) are true c) only (iii) is true d) (iii) and (iv) are true 3. Which of the following symbol is used in the place of except? a) ^ b) V c) ¬ d) ~ 4. Which of the following is the comparison operator in tuple relational calculus 1.⇒ b) = c) ε d) All of the mentioned 5. In tuple relational calculus P1 → P2 is equivalent to a)¬P1 ∨ P2 b)¬P1 ∨ P2 c)P1 ∧ P2 d)P1 ∧ ¬P2
  • 27. Department of Information Technology 27Data base Technologies (ITB4201) Answers 1. Because of the calculus expression, the relational calculus in considered as a) procedural language b) non procedural language c) structural language d) functional language 2. Relational calculus is: i. equivalent to relational algebra in its capabilities. Ii. It is stronger than relational algebra Iii. It is weaker than relational algebra. Iv. It is based on predicate calculus of formal logic a) (i) and (iv) are true b) (ii) and (iv) are true c) only (iii) is true d) (iii) and (iv) are true 3. Which of the following symbol is used in the place of except? a) ^ b) V c) ¬ d) ~ 4. Which of the following is the comparison operator in tuple relational calculus 1.⇒ b) = c) ε d) All of the mentioned 5. In tuple relational calculus P1 → P2 is equivalent to a)¬P1 ∨ P2 b)¬P1 ∨ P2 c)P1 ∧ P2 d)P1 ∧ ¬P2