SlideShare a Scribd company logo
1 of 21
Download to read offline
1
Lecture 5:
Relational calculus
www.cl.cam.ac.uk/Teaching/current/Databases/
2
Relational calculus
• There are two versions of the relational
calculus:
– Tuple relational calculus (TRC)
– Domain relational calculus (DRC)
• Both TRC and DRC are simple subsets of first-
order logic
• The difference is the level at which variables are
used: for fields (domains) or for tuples
• The calculus is non-procedural (‘declarative’)
compared to the relational algebra
3
Domain relational
calculus
• Queries have the form
{<x1,…,xn>| F(x1,…,xn)}
where x1,…,xn are domain variables and F
is a formula with free variables {x1,…,xn}
• Answer: all tuples <v1,…,vn> that make
F(v1,…,vn) true
4
Example
Find all sailors with a rating above 7
{<I,N,R,A> | <I,N,R,A>∈Sailors ∧ R>7}
• The condition <I,N,R,A>∈Sailors ensures
that the domain variables are bound to the
appropriate fields of the Sailors tuple
5
Example
• Simple projection:
{<I,N> | ∃R,A.<I,N,R,A>∈Sailors}
• Simple projection and selection:
{<I,N> | ∃R,A.<I,N,R,A>∈Sailors ∧ N=`Julia’}
6
DRC formulae
• Atomic formulae: a ::=
– <x1,…,xn>∈R
– xi binop xj, xi binop c, c binop xj, unop c, unop
xi
• DRC Formulae: P, Q ::=
– a
– ¬P, P∧Q, P∨Q
– ∃x.P
– ∀x.P
• Recall that ∃x and ∀x are binders for x
7
Example
Find the names of sailors rated >7 who’ve
reserved boat 103
{<N> | ∃I,A,R.<I,N,R,A>∈Sailors ∧
R>7 ∧
∃SI,BI,D.(<SI,BI,D>∈Reserves ∧
I=SI ∧ BI=103)}
• Note the use of ∃ and = to ‘simulate’ join
8
Example
Find the names of sailors rated >7 who’ve
reserved a red boat
{<N> | ∃I,A,R.<I,N,R,A>∈Sailors ∧
R>7 ∧
∃SI,BI,D. (<SI,BI,D>∈Reserves ∧
SI=I ∧
∃B,C. (<B,C>∈Boats ∧
B=BI ∧ C=‘red’))}
9
Example
Find the names of sailors who have
reserved at least two boats
{<N> | ∃I,R,A. <I,N,R,A>∈Sailors ∧
∃BI1,BI2,D1,D2.<I,BI1,D1>∈Reserves ∧
<I,BI2,D2>∈Reserves ∧
BI1≠BI2 }
10
Example
Find names of sailors who’ve reserved all
boats
11
Example
Find names of sailors who’ve reserved all boats
{<N> | ∃I,R,A. <I,N,R,A>∈Sailors ∧
∀B,C. (¬(<B,C>∈Boats) ∨
(∃<SI,BI,D>∈Reserves.
I=SI ∧ BI=B)) }
{<N> | ∃I,R,A. <I,N,R,A>∈Sailors ∧
∀<B,C>∈Boats.
∃<SI,BI,D>∈Reserves.
I=SI ∧ BI=B)) }
12
Tuple relational calculus
• Similar to DRC except that variables range
over tuples rather than field values
• For example, the query “Find all sailors
with rating above 7” is represented in TRC
as follows:
{S | S∈Sailors ∧ S.rating>7}
13
Semantics of TRC queries
• In general a TRC query is of the form
{t | P}
where FV(P)={t}
• The answer to such a query is the set of
all tuples T for which P[T/t] is true
14
Example
Find names and ages of sailors with a rating
above 7
{P | ∃S∈Sailors. S.rating>7∧
P.sname=S.sname∧
P.age=S.age}
Recall P ranges
over tuple values
15
Example
Find the names of sailors who have
reserved at least two boats
{ P | ∃S∈Sailors.
∃R1∈Reserves. ∃R2∈Reserves.
S.sid=R1.sid ∧ R1.sid=R2.sid ∧
R1.bid ≠ R2.bid ∧
P.sname=S.sname}
17
Equivalence with
relational algebra
• This equivalence was first considered by
Codd in 1972
• Codd introduced the notion of relational
completeness
– A language is relationally complete if it can
express all the queries expressible in the
relational algebra.
18
Encoding relational
algebra
• Let’s consider the first direction of the
equivalence: can the relational algebra be
coded up in the (domain) relational
calculus?
• This translation can be done
systematically, we define a translation
function [-]
• Simple case:
[R] = {<x1,…,xn> | <x1,…,xn>∈R}
19
Encoding selection
• Assume
[e] = {<x1,…,xn> | F }
• Then
[σc(e)] = {<x1,…,xn> | F ∧ C’}
where C’ is obtained from C by replacing
each attribute with the corresponding
variable
20
Encoding relational
calculus
• Can we code up the relational calculus in
the relational algebra?
• At the moment, NO!
• Given our syntax we can define
‘problematic’ queries such as
{S | ¬ (S∈Sailors)}
• This (presumably) means the set of all
tuples that are not sailors, which is an
infinite set… 
21
Safe queries
• A query is said to be safe if no matter how we instantiate
the relations, it always produces a finite answer
• Unfortunately, safety (a semantic condition) is
undecidable 
– That is, given a arbitrary query, no program can decide if it is
safe
• Fortunately, we can define a restricted syntactic class of
queries which are guaranteed to be safe 
• Safe queries can be encoded in the relational algebra
22
Summary
You should now understand
• The relational calculus
– Tuple relational calculus
– Domain relational calculus
• Translation from relational algebra to
relational calculus
• Safe queries and relational completeness
Next lecture: Basic SQL

More Related Content

Similar to 6 - Relational Calculus.pdf

Relational calculas
Relational calculasRelational calculas
Relational calculasanuj24
 
ON RUN-LENGTH-CONSTRAINED BINARY SEQUENCES
ON RUN-LENGTH-CONSTRAINED BINARY SEQUENCESON RUN-LENGTH-CONSTRAINED BINARY SEQUENCES
ON RUN-LENGTH-CONSTRAINED BINARY SEQUENCESijitjournal
 
snarks <3 hash functions
snarks <3 hash functionssnarks <3 hash functions
snarks <3 hash functionsRebekah Mercer
 
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...Alex Pruden
 
Computational Complexity: Complexity Classes
Computational Complexity: Complexity ClassesComputational Complexity: Complexity Classes
Computational Complexity: Complexity ClassesAntonis Antonopoulos
 
Mathsclass xii (exampler problems)
Mathsclass xii (exampler problems)Mathsclass xii (exampler problems)
Mathsclass xii (exampler problems)nitishguptamaps
 
Text classification using Text kernels
Text classification using Text kernelsText classification using Text kernels
Text classification using Text kernelsDev Nath
 
MTH101 - Calculus and Analytical Geometry- Lecture 42
MTH101 - Calculus and Analytical Geometry- Lecture 42MTH101 - Calculus and Analytical Geometry- Lecture 42
MTH101 - Calculus and Analytical Geometry- Lecture 42Bilal Ahmed
 
Integral Calculus Anti Derivatives reviewer
Integral Calculus Anti Derivatives reviewerIntegral Calculus Anti Derivatives reviewer
Integral Calculus Anti Derivatives reviewerJoshuaAgcopra
 
M1 unit v-jntuworld
M1 unit v-jntuworldM1 unit v-jntuworld
M1 unit v-jntuworldmrecedu
 
Talk on Resource Allocation Strategies for Layered Multimedia Multicast Services
Talk on Resource Allocation Strategies for Layered Multimedia Multicast ServicesTalk on Resource Allocation Strategies for Layered Multimedia Multicast Services
Talk on Resource Allocation Strategies for Layered Multimedia Multicast ServicesAndrea Tassi
 
Intro To Formula Project
Intro To Formula ProjectIntro To Formula Project
Intro To Formula ProjectKathy Favazza
 
RedisDay London 2018 - CRDTs and Redis From sequential to concurrent executions
RedisDay London 2018 - CRDTs and Redis From sequential to concurrent executionsRedisDay London 2018 - CRDTs and Redis From sequential to concurrent executions
RedisDay London 2018 - CRDTs and Redis From sequential to concurrent executionsRedis Labs
 

Similar to 6 - Relational Calculus.pdf (20)

Relational calculas
Relational calculasRelational calculas
Relational calculas
 
ON RUN-LENGTH-CONSTRAINED BINARY SEQUENCES
ON RUN-LENGTH-CONSTRAINED BINARY SEQUENCESON RUN-LENGTH-CONSTRAINED BINARY SEQUENCES
ON RUN-LENGTH-CONSTRAINED BINARY SEQUENCES
 
snarks <3 hash functions
snarks <3 hash functionssnarks <3 hash functions
snarks <3 hash functions
 
A superglue for string comparison
A superglue for string comparisonA superglue for string comparison
A superglue for string comparison
 
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
 
Math
MathMath
Math
 
AppsDiff3c.pdf
AppsDiff3c.pdfAppsDiff3c.pdf
AppsDiff3c.pdf
 
Computational Complexity: Complexity Classes
Computational Complexity: Complexity ClassesComputational Complexity: Complexity Classes
Computational Complexity: Complexity Classes
 
Mathsclass xii (exampler problems)
Mathsclass xii (exampler problems)Mathsclass xii (exampler problems)
Mathsclass xii (exampler problems)
 
Relational Calculus
Relational CalculusRelational Calculus
Relational Calculus
 
Text classification using Text kernels
Text classification using Text kernelsText classification using Text kernels
Text classification using Text kernels
 
MTH101 - Calculus and Analytical Geometry- Lecture 42
MTH101 - Calculus and Analytical Geometry- Lecture 42MTH101 - Calculus and Analytical Geometry- Lecture 42
MTH101 - Calculus and Analytical Geometry- Lecture 42
 
Integral Calculus Anti Derivatives reviewer
Integral Calculus Anti Derivatives reviewerIntegral Calculus Anti Derivatives reviewer
Integral Calculus Anti Derivatives reviewer
 
M1 unit v-jntuworld
M1 unit v-jntuworldM1 unit v-jntuworld
M1 unit v-jntuworld
 
Talk on Resource Allocation Strategies for Layered Multimedia Multicast Services
Talk on Resource Allocation Strategies for Layered Multimedia Multicast ServicesTalk on Resource Allocation Strategies for Layered Multimedia Multicast Services
Talk on Resource Allocation Strategies for Layered Multimedia Multicast Services
 
Intro To Formula Project
Intro To Formula ProjectIntro To Formula Project
Intro To Formula Project
 
25010001
2501000125010001
25010001
 
Unit 3
Unit 3Unit 3
Unit 3
 
Unit 3
Unit 3Unit 3
Unit 3
 
RedisDay London 2018 - CRDTs and Redis From sequential to concurrent executions
RedisDay London 2018 - CRDTs and Redis From sequential to concurrent executionsRedisDay London 2018 - CRDTs and Redis From sequential to concurrent executions
RedisDay London 2018 - CRDTs and Redis From sequential to concurrent executions
 

Recently uploaded

April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
ALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxolyaivanovalion
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxolyaivanovalion
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...shambhavirathore45
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxMohammedJunaid861692
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramMoniSankarHazra
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfadriantubila
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% SecurePooja Nehwal
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightDelhi Call girls
 
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service OnlineCALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Onlineanilsa9823
 

Recently uploaded (20)

April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
ALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptx
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics Program
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service OnlineCALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
 

6 - Relational Calculus.pdf

  • 2. 2 Relational calculus • There are two versions of the relational calculus: – Tuple relational calculus (TRC) – Domain relational calculus (DRC) • Both TRC and DRC are simple subsets of first- order logic • The difference is the level at which variables are used: for fields (domains) or for tuples • The calculus is non-procedural (‘declarative’) compared to the relational algebra
  • 3. 3 Domain relational calculus • Queries have the form {<x1,…,xn>| F(x1,…,xn)} where x1,…,xn are domain variables and F is a formula with free variables {x1,…,xn} • Answer: all tuples <v1,…,vn> that make F(v1,…,vn) true
  • 4. 4 Example Find all sailors with a rating above 7 {<I,N,R,A> | <I,N,R,A>∈Sailors ∧ R>7} • The condition <I,N,R,A>∈Sailors ensures that the domain variables are bound to the appropriate fields of the Sailors tuple
  • 5. 5 Example • Simple projection: {<I,N> | ∃R,A.<I,N,R,A>∈Sailors} • Simple projection and selection: {<I,N> | ∃R,A.<I,N,R,A>∈Sailors ∧ N=`Julia’}
  • 6. 6 DRC formulae • Atomic formulae: a ::= – <x1,…,xn>∈R – xi binop xj, xi binop c, c binop xj, unop c, unop xi • DRC Formulae: P, Q ::= – a – ¬P, P∧Q, P∨Q – ∃x.P – ∀x.P • Recall that ∃x and ∀x are binders for x
  • 7. 7 Example Find the names of sailors rated >7 who’ve reserved boat 103 {<N> | ∃I,A,R.<I,N,R,A>∈Sailors ∧ R>7 ∧ ∃SI,BI,D.(<SI,BI,D>∈Reserves ∧ I=SI ∧ BI=103)} • Note the use of ∃ and = to ‘simulate’ join
  • 8. 8 Example Find the names of sailors rated >7 who’ve reserved a red boat {<N> | ∃I,A,R.<I,N,R,A>∈Sailors ∧ R>7 ∧ ∃SI,BI,D. (<SI,BI,D>∈Reserves ∧ SI=I ∧ ∃B,C. (<B,C>∈Boats ∧ B=BI ∧ C=‘red’))}
  • 9. 9 Example Find the names of sailors who have reserved at least two boats {<N> | ∃I,R,A. <I,N,R,A>∈Sailors ∧ ∃BI1,BI2,D1,D2.<I,BI1,D1>∈Reserves ∧ <I,BI2,D2>∈Reserves ∧ BI1≠BI2 }
  • 10. 10 Example Find names of sailors who’ve reserved all boats
  • 11. 11 Example Find names of sailors who’ve reserved all boats {<N> | ∃I,R,A. <I,N,R,A>∈Sailors ∧ ∀B,C. (¬(<B,C>∈Boats) ∨ (∃<SI,BI,D>∈Reserves. I=SI ∧ BI=B)) } {<N> | ∃I,R,A. <I,N,R,A>∈Sailors ∧ ∀<B,C>∈Boats. ∃<SI,BI,D>∈Reserves. I=SI ∧ BI=B)) }
  • 12. 12 Tuple relational calculus • Similar to DRC except that variables range over tuples rather than field values • For example, the query “Find all sailors with rating above 7” is represented in TRC as follows: {S | S∈Sailors ∧ S.rating>7}
  • 13. 13 Semantics of TRC queries • In general a TRC query is of the form {t | P} where FV(P)={t} • The answer to such a query is the set of all tuples T for which P[T/t] is true
  • 14. 14 Example Find names and ages of sailors with a rating above 7 {P | ∃S∈Sailors. S.rating>7∧ P.sname=S.sname∧ P.age=S.age} Recall P ranges over tuple values
  • 15. 15 Example Find the names of sailors who have reserved at least two boats { P | ∃S∈Sailors. ∃R1∈Reserves. ∃R2∈Reserves. S.sid=R1.sid ∧ R1.sid=R2.sid ∧ R1.bid ≠ R2.bid ∧ P.sname=S.sname}
  • 16. 17 Equivalence with relational algebra • This equivalence was first considered by Codd in 1972 • Codd introduced the notion of relational completeness – A language is relationally complete if it can express all the queries expressible in the relational algebra.
  • 17. 18 Encoding relational algebra • Let’s consider the first direction of the equivalence: can the relational algebra be coded up in the (domain) relational calculus? • This translation can be done systematically, we define a translation function [-] • Simple case: [R] = {<x1,…,xn> | <x1,…,xn>∈R}
  • 18. 19 Encoding selection • Assume [e] = {<x1,…,xn> | F } • Then [σc(e)] = {<x1,…,xn> | F ∧ C’} where C’ is obtained from C by replacing each attribute with the corresponding variable
  • 19. 20 Encoding relational calculus • Can we code up the relational calculus in the relational algebra? • At the moment, NO! • Given our syntax we can define ‘problematic’ queries such as {S | ¬ (S∈Sailors)} • This (presumably) means the set of all tuples that are not sailors, which is an infinite set… 
  • 20. 21 Safe queries • A query is said to be safe if no matter how we instantiate the relations, it always produces a finite answer • Unfortunately, safety (a semantic condition) is undecidable  – That is, given a arbitrary query, no program can decide if it is safe • Fortunately, we can define a restricted syntactic class of queries which are guaranteed to be safe  • Safe queries can be encoded in the relational algebra
  • 21. 22 Summary You should now understand • The relational calculus – Tuple relational calculus – Domain relational calculus • Translation from relational algebra to relational calculus • Safe queries and relational completeness Next lecture: Basic SQL