SlideShare uma empresa Scribd logo
1 de 18
Relational Calculus
    CS 186, Spring 2006, Lecture 9
                   R&G, Chapter 4




We will occasionally use this
arrow notation unless there
is danger of no confusion.
          Ronald Graham
            Elements of Ramsey Theory
Relational Calculus
• Comes in two flavors: Tuple relational calculus (TRC)
  and Domain relational calculus (DRC).
• Calculus has variables, constants, comparison ops,
  logical connectives and quantifiers.
   – TRC: Variables range over (i.e., get bound to) tuples.
      • Like SQL.
   – DRC: Variables range over domain elements (= field
     values).
      • Like Query-By-Example (QBE)
   – Both TRC and DRC are simple subsets of first-order logic.
• Expressions in the calculus are called formulas.
• Answer tuple is an assignment of constants to
  variables that make the formula evaluate to true.
Tuple Relational Calculus
• Query has the form: {T | p(T)}
   – p(T) denotes a formula in which tuple
     variable T appears.
• Answer is the set of all tuples T for
  which the formula p(T) evaluates to true.
• Formula is recursively defined:
   start with simple atomic formulas (get
     tuples from relations or make comparisons of
     values)
   build bigger and better formulas using the
     logical connectives.
TRC Formulas
• An Atomic formula is one of the following:
  R Rel
  R.a op S.b
  R.a op constant
     op is one of      , , , , ,
• A formula can be:
  – an atomic formula
  –     p, p    q, p q where p and q are formulas
  –    R ( p ( R )) where variable R is a tuple variable
  –    R ( p ( R )) where variable R is a tuple variable
Free and Bound Variables

• The use of quantifiers       X and X in a formula is
  said to bind X in the formula.
   – A variable that is not bound is free.
• Let us revisit the definition of a query:
    – {T | p(T)}

•   There is an important restriction
    — the variable T that appears to the left of `|’ must be
      the only free variable in the formula p(T).
    — in other words, all other tuple variables must be
      bound using a quantifier.
Selection and Projection
• Find all sailors with rating above 7
              {S |S Sailors          S.rating > 7}
   – Modify this query to answer: Find sailors who are older
     than 18 or have a rating under 9, and are called ‘Bob’.
• Find names and ages of sailors with rating above 7.
      {S | S1 Sailors(S1.rating > 7
                       S.sname = S1.sname
                       S.age = S1.age)}
Note, here S is a tuple variable of 2 fields (i.e. {S} is a
projection of sailors), since only 2 fields are ever mentioned
and S is never used to range over any relations in the query.
Joins
Find sailors rated > 7 who’ve reserved boat
  #103

  {S | S Sailors S.rating > 7
        R(R Reserves R.sid = S.sid
            R.bid = 103)}
  Note the use of to find a tuple in Reserves
   that `joins with’ the Sailors tuple under
   consideration.
Joins (continued)
Find sailors rated > 7 who’ve reserved boat #103
     {S | S Sailors S.rating > 7
           R(R Reserves R.sid = S.sid
               R.bid = 103)}
Find sailors rated > 7 who’ve reserved a red boat
   {S | S Sailors S.rating > 7
         R(R Reserves R.sid = S.sid
              B(B Boats B.bid = R.bid
                  B.color = „red‟))}
• Observe how the parentheses control the scope of
  each quantifier’s binding. (Similar to SQL!)
Division (makes more sense here???)
  Find sailors who’ve reserved all boats
     (hint, use )
   {S | S Sailors
         B Boats ( R Reserves
                    (S.sid = R.sid
                       B.bid = R.bid))}
• Find all sailors S such that for each tuple B in Boats
  there is a tuple in Reserves showing that sailor S has
  reserved it.
Division – a trickier example…
Find sailors who‟ve reserved all Red boats
       {S | S Sailors
              B Boats ( B.color = ‘red’
             R(R Reserves S.sid = R.sid
                 B.bid = R.bid))}
Alternatively…
        {S | S Sailors
              B Boats ( B.color ‘red’
              R(R Reserves S.sid = R.sid
                 B.bid = R.bid))}
a       b is the same as a              b

             b       • If a is true, b must be
                       true for the implication
        T        F     to be true. If a is true
                       and b is false, the
    T   T        F     implication evaluates to
                       false.
a                    • If a is not true, we don’t
                       care about b, the
    F   T        T     expression is always
                       true.
Unsafe Queries, Expressive Power
•      syntactically correct calculus queries that have
    an infinite number of answers! Unsafe queries.
     – e.g., S |   S Sailors

   – 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 language (e.g.,
  SQL) can express every query that is expressible in
  relational algebra/calculus. (actually, SQL is more
  powerful, as we will see…)
Summary
• The relational model has rigorously defined query
  languages — simple and powerful.
• Relational algebra is more operational
   – useful as internal representation for query evaluation plans.
• Relational calculus is non-operational
   – users define queries in terms of what they want, not in
     terms of how to compute it. (Declarative)
• Several ways of expressing a given query
   – a query optimizer should choose the most efficient version.
• Algebra and safe calculus have same expressive power
   – leads to the notion of relational completeness.
Midterm I - Info
•   Remember - Lectures, Sections, Book & HW1
•   1 Cheat Sheet (2 sided, 8.5x11) - No electronics.
•   Tues 2/21 in class
•   Topics: next
Midterm I - Topics
• Ch 1 - Introduction - all sections
• Ch 3 - Relational Model - 3.1 thru 3.4

•   Ch 9 - Disks and Files - all except 9.2 (RAID)
•   Ch 8 - Storage & Indexing - all
•   Ch 10 - Tree-based IXs - all
•   Ch 11 - Hash-based IXs - all

• Ch 4 - Rel Alg & Calc - all (except DRC 4.3.2)
Addendum: Use of

• x (P(x)) - is only true if P(x) is true for
  every x in the universe
• Usually:
     x ((x Boats) (x.color = “Red”)
• logical implication,
   a b means that if a is true, b must be
    true
   a b is the same as a b
Find sailors who’ve reserved all boats
       {S | S Sailors
             B( (B Boats)
             R(R Reserves S.sid = R.sid
                 B.bid = R.bid))}
• Find all sailors S such that for each tuple B
  either it is not a tuple in Boats or there is a tuple in
  Reserves showing that sailor S has reserved it.
       {S | S Sailors
             B( (B Boats)
             R(R Reserves S.sid = R.sid
                B.bid = R.bid))}
... reserved all red boats
       {S | S Sailors
             B( (B Boats B.color = “red”)
             R(R Reserves S.sid = R.sid
                 B.bid = R.bid))}
• Find all sailors S such that for each tuple B
  either it is not a tuple in Boats or there is a tuple in
  Reserves showing that sailor S has reserved it.
       {S | S Sailors
             B( (B Boats) (B.color “red”)
             R(R Reserves S.sid = R.sid
                B.bid = R.bid))}

Mais conteúdo relacionado

Mais procurados

context free language
context free languagecontext free language
context free languagekhush_boo31
 
Concurrency Conrol
Concurrency ConrolConcurrency Conrol
Concurrency Conrollubna19
 
Modeling Physical Systems in the Metaverse Easily with Graphs
Modeling Physical Systems in the Metaverse Easily with GraphsModeling Physical Systems in the Metaverse Easily with Graphs
Modeling Physical Systems in the Metaverse Easily with GraphsNeo4j
 
Virtual Memory
Virtual MemoryVirtual Memory
Virtual Memoryvampugani
 
Database ,11 Concurrency Control
Database ,11 Concurrency ControlDatabase ,11 Concurrency Control
Database ,11 Concurrency ControlAli Usman
 
Ch17 introduction to transaction processing concepts and theory
Ch17 introduction to transaction processing concepts and theoryCh17 introduction to transaction processing concepts and theory
Ch17 introduction to transaction processing concepts and theorymeenatchi selvaraj
 
Functions in discrete mathematics
Functions in discrete mathematicsFunctions in discrete mathematics
Functions in discrete mathematicsRachana Pathak
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR Zahid Parvez
 
Principles of Programming Languages - Lecture Notes
Principles of Programming Languages -  Lecture NotesPrinciples of Programming Languages -  Lecture Notes
Principles of Programming Languages - Lecture Notessuthi
 
Multiversion Concurrency Control Techniques
Multiversion Concurrency Control TechniquesMultiversion Concurrency Control Techniques
Multiversion Concurrency Control TechniquesRaj vardhan
 
Ch3 4 regular expression and grammar
Ch3 4 regular expression and grammarCh3 4 regular expression and grammar
Ch3 4 regular expression and grammarmeresie tesfay
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computationBipul Roy Bpl
 
Lock based protocols
Lock based protocolsLock based protocols
Lock based protocolsChethanMp7
 
Semantic Analysis.pptx
Semantic Analysis.pptxSemantic Analysis.pptx
Semantic Analysis.pptxZarfaMasood
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automatahafizhamza0322
 

Mais procurados (20)

context free language
context free languagecontext free language
context free language
 
Concurrency Conrol
Concurrency ConrolConcurrency Conrol
Concurrency Conrol
 
Modeling Physical Systems in the Metaverse Easily with Graphs
Modeling Physical Systems in the Metaverse Easily with GraphsModeling Physical Systems in the Metaverse Easily with Graphs
Modeling Physical Systems in the Metaverse Easily with Graphs
 
Virtual Memory
Virtual MemoryVirtual Memory
Virtual Memory
 
Database ,11 Concurrency Control
Database ,11 Concurrency ControlDatabase ,11 Concurrency Control
Database ,11 Concurrency Control
 
Ch17 introduction to transaction processing concepts and theory
Ch17 introduction to transaction processing concepts and theoryCh17 introduction to transaction processing concepts and theory
Ch17 introduction to transaction processing concepts and theory
 
Functions in discrete mathematics
Functions in discrete mathematicsFunctions in discrete mathematics
Functions in discrete mathematics
 
Coal 11 jumps in Assembly Programming
Coal 11 jumps in Assembly ProgrammingCoal 11 jumps in Assembly Programming
Coal 11 jumps in Assembly Programming
 
Pumping lemma
Pumping lemmaPumping lemma
Pumping lemma
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
 
Principles of Programming Languages - Lecture Notes
Principles of Programming Languages -  Lecture NotesPrinciples of Programming Languages -  Lecture Notes
Principles of Programming Languages - Lecture Notes
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Multiversion Concurrency Control Techniques
Multiversion Concurrency Control TechniquesMultiversion Concurrency Control Techniques
Multiversion Concurrency Control Techniques
 
Ch3 4 regular expression and grammar
Ch3 4 regular expression and grammarCh3 4 regular expression and grammar
Ch3 4 regular expression and grammar
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
 
Lock based protocols
Lock based protocolsLock based protocols
Lock based protocols
 
Semantic Analysis.pptx
Semantic Analysis.pptxSemantic Analysis.pptx
Semantic Analysis.pptx
 
Three Address code
Three Address code Three Address code
Three Address code
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
 

Destaque

Nested quantifiers
Nested quantifiersNested quantifiers
Nested quantifiersAli Saleem
 
Nested quantifiers
Nested quantifiersNested quantifiers
Nested quantifiersAli Saleem
 
Predicate calculus
Predicate calculusPredicate calculus
Predicate calculusRajendran
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbmsshekhar1991
 
Predicate Logic
Predicate LogicPredicate Logic
Predicate Logicgiki67
 
Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)yourbookworldanil
 
Lecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusLecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusemailharmeet
 

Destaque (7)

Nested quantifiers
Nested quantifiersNested quantifiers
Nested quantifiers
 
Nested quantifiers
Nested quantifiersNested quantifiers
Nested quantifiers
 
Predicate calculus
Predicate calculusPredicate calculus
Predicate calculus
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
Predicate Logic
Predicate LogicPredicate Logic
Predicate Logic
 
Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)
 
Lecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusLecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculus
 

Semelhante a Relational calculas

lecture9Calc.ppt
lecture9Calc.pptlecture9Calc.ppt
lecture9Calc.pptrituah
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015Edhole.com
 
Chapter-7 Relational Calculus
Chapter-7 Relational CalculusChapter-7 Relational Calculus
Chapter-7 Relational CalculusKunal Anand
 
lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...
lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...
lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...RajuNestham1
 
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
 
6 - Relational Calculus.pdf
6 - Relational Calculus.pdf6 - Relational Calculus.pdf
6 - Relational Calculus.pdfgaurav70287
 
leanCoR: lean Connection-based DL Reasoner
leanCoR: lean Connection-based DL ReasonerleanCoR: lean Connection-based DL Reasoner
leanCoR: lean Connection-based DL ReasonerAdriano Melo
 
Design of databases
Design of databasesDesign of databases
Design of databasesHava91
 
Geometry Section 6-5
Geometry Section 6-5Geometry Section 6-5
Geometry Section 6-5Jimbo Lamb
 
Relations and functions
Relations and functions Relations and functions
Relations and functions Seyid Kadher
 
Mesh Shape Editing
Mesh Shape EditingMesh Shape Editing
Mesh Shape Editingssuser24ddad
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languagesparmeet834
 

Semelhante a Relational calculas (20)

lecture9Calc.ppt
lecture9Calc.pptlecture9Calc.ppt
lecture9Calc.ppt
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
 
lecture05-14f.ppt
lecture05-14f.pptlecture05-14f.ppt
lecture05-14f.ppt
 
Chapter-7 Relational Calculus
Chapter-7 Relational CalculusChapter-7 Relational Calculus
Chapter-7 Relational Calculus
 
lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...
lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...
lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...
 
lecture8Alg.ppt
lecture8Alg.pptlecture8Alg.ppt
lecture8Alg.ppt
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
Relational Algebra and Calculus.ppt
Relational Algebra and Calculus.pptRelational Algebra and Calculus.ppt
Relational Algebra and Calculus.ppt
 
6 - Relational Calculus.pdf
6 - Relational Calculus.pdf6 - Relational Calculus.pdf
6 - Relational Calculus.pdf
 
Transformations
TransformationsTransformations
Transformations
 
asal12 sqliii
asal12 sqliiiasal12 sqliii
asal12 sqliii
 
leanCoR: lean Connection-based DL Reasoner
leanCoR: lean Connection-based DL ReasonerleanCoR: lean Connection-based DL Reasoner
leanCoR: lean Connection-based DL Reasoner
 
Design of databases
Design of databasesDesign of databases
Design of databases
 
Geometry Section 6-5
Geometry Section 6-5Geometry Section 6-5
Geometry Section 6-5
 
Relations and functions
Relations and functions Relations and functions
Relations and functions
 
8.4 notes
8.4 notes8.4 notes
8.4 notes
 
Mesh Shape Editing
Mesh Shape EditingMesh Shape Editing
Mesh Shape Editing
 
Elhabian_curves10.pdf
Elhabian_curves10.pdfElhabian_curves10.pdf
Elhabian_curves10.pdf
 
Triangles ix
Triangles ixTriangles ix
Triangles ix
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languages
 

Último

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 

Último (20)

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
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
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

Relational calculas

  • 1. Relational Calculus CS 186, Spring 2006, Lecture 9 R&G, Chapter 4 We will occasionally use this arrow notation unless there is danger of no confusion. Ronald Graham Elements of Ramsey Theory
  • 2. Relational Calculus • Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). • Calculus has variables, constants, comparison ops, logical connectives and quantifiers. – TRC: Variables range over (i.e., get bound to) tuples. • Like SQL. – DRC: Variables range over domain elements (= field values). • Like Query-By-Example (QBE) – Both TRC and DRC are simple subsets of first-order logic. • Expressions in the calculus are called formulas. • Answer tuple is an assignment of constants to variables that make the formula evaluate to true.
  • 3. Tuple Relational Calculus • Query has the form: {T | p(T)} – p(T) denotes a formula in which tuple variable T appears. • Answer is the set of all tuples T for which the formula p(T) evaluates to true. • Formula is recursively defined: start with simple atomic formulas (get tuples from relations or make comparisons of values) build bigger and better formulas using the logical connectives.
  • 4. TRC Formulas • An Atomic formula is one of the following: R Rel R.a op S.b R.a op constant op is one of , , , , , • A formula can be: – an atomic formula – p, p q, p q where p and q are formulas – R ( p ( R )) where variable R is a tuple variable – R ( p ( R )) where variable R is a tuple variable
  • 5. Free and Bound Variables • The use of quantifiers X and X in a formula is said to bind X in the formula. – A variable that is not bound is free. • Let us revisit the definition of a query: – {T | p(T)} • There is an important restriction — the variable T that appears to the left of `|’ must be the only free variable in the formula p(T). — in other words, all other tuple variables must be bound using a quantifier.
  • 6. Selection and Projection • Find all sailors with rating above 7 {S |S Sailors S.rating > 7} – Modify this query to answer: Find sailors who are older than 18 or have a rating under 9, and are called ‘Bob’. • Find names and ages of sailors with rating above 7. {S | S1 Sailors(S1.rating > 7 S.sname = S1.sname S.age = S1.age)} Note, here S is a tuple variable of 2 fields (i.e. {S} is a projection of sailors), since only 2 fields are ever mentioned and S is never used to range over any relations in the query.
  • 7. Joins Find sailors rated > 7 who’ve reserved boat #103 {S | S Sailors S.rating > 7 R(R Reserves R.sid = S.sid R.bid = 103)} Note the use of to find a tuple in Reserves that `joins with’ the Sailors tuple under consideration.
  • 8. Joins (continued) Find sailors rated > 7 who’ve reserved boat #103 {S | S Sailors S.rating > 7 R(R Reserves R.sid = S.sid R.bid = 103)} Find sailors rated > 7 who’ve reserved a red boat {S | S Sailors S.rating > 7 R(R Reserves R.sid = S.sid B(B Boats B.bid = R.bid B.color = „red‟))} • Observe how the parentheses control the scope of each quantifier’s binding. (Similar to SQL!)
  • 9. Division (makes more sense here???) Find sailors who’ve reserved all boats (hint, use ) {S | S Sailors B Boats ( R Reserves (S.sid = R.sid B.bid = R.bid))} • Find all sailors S such that for each tuple B in Boats there is a tuple in Reserves showing that sailor S has reserved it.
  • 10. Division – a trickier example… Find sailors who‟ve reserved all Red boats {S | S Sailors B Boats ( B.color = ‘red’ R(R Reserves S.sid = R.sid B.bid = R.bid))} Alternatively… {S | S Sailors B Boats ( B.color ‘red’ R(R Reserves S.sid = R.sid B.bid = R.bid))}
  • 11. a b is the same as a b b • If a is true, b must be true for the implication T F to be true. If a is true and b is false, the T T F implication evaluates to false. a • If a is not true, we don’t care about b, the F T T expression is always true.
  • 12. Unsafe Queries, Expressive Power • syntactically correct calculus queries that have an infinite number of answers! Unsafe queries. – e.g., S | S Sailors – 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 language (e.g., SQL) can express every query that is expressible in relational algebra/calculus. (actually, SQL is more powerful, as we will see…)
  • 13. Summary • The relational model has rigorously defined query languages — simple and powerful. • Relational algebra is more operational – useful as internal representation for query evaluation plans. • Relational calculus is non-operational – users define queries in terms of what they want, not in terms of how to compute it. (Declarative) • Several ways of expressing a given query – a query optimizer should choose the most efficient version. • Algebra and safe calculus have same expressive power – leads to the notion of relational completeness.
  • 14. Midterm I - Info • Remember - Lectures, Sections, Book & HW1 • 1 Cheat Sheet (2 sided, 8.5x11) - No electronics. • Tues 2/21 in class • Topics: next
  • 15. Midterm I - Topics • Ch 1 - Introduction - all sections • Ch 3 - Relational Model - 3.1 thru 3.4 • Ch 9 - Disks and Files - all except 9.2 (RAID) • Ch 8 - Storage & Indexing - all • Ch 10 - Tree-based IXs - all • Ch 11 - Hash-based IXs - all • Ch 4 - Rel Alg & Calc - all (except DRC 4.3.2)
  • 16. Addendum: Use of • x (P(x)) - is only true if P(x) is true for every x in the universe • Usually: x ((x Boats) (x.color = “Red”) • logical implication, a b means that if a is true, b must be true a b is the same as a b
  • 17. Find sailors who’ve reserved all boats {S | S Sailors B( (B Boats) R(R Reserves S.sid = R.sid B.bid = R.bid))} • Find all sailors S such that for each tuple B either it is not a tuple in Boats or there is a tuple in Reserves showing that sailor S has reserved it. {S | S Sailors B( (B Boats) R(R Reserves S.sid = R.sid B.bid = R.bid))}
  • 18. ... reserved all red boats {S | S Sailors B( (B Boats B.color = “red”) R(R Reserves S.sid = R.sid B.bid = R.bid))} • Find all sailors S such that for each tuple B either it is not a tuple in Boats or there is a tuple in Reserves showing that sailor S has reserved it. {S | S Sailors B( (B Boats) (B.color “red”) R(R Reserves S.sid = R.sid B.bid = R.bid))}