SlideShare une entreprise Scribd logo
1  sur  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
– where p and q are formulas
– where variable R is a tuple variable
– where variable R is a tuple variable
     
, , , , ,
  
p p q p q
, ,
)
)
(
( R
p
R

)
)
(
( R
p
R

Free and Bound Variables
• The use of quantifiers and 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)}
 X  X
• 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 names and ages of sailors with rating above 7.
{S |S Sailors  S.rating > 7}
{S | S1 Sailors(S1.rating > 7
 S.sname = S1.sname
 S.age = S1.age)}
– Modify this query to answer: Find sailors who are older
than 18 or have a rating under 9, and are called ‘Bob’.
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.
• Find all sailors with rating above 7
Find sailors rated > 7 who’ve reserved boat
#103
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
Joins (continued)
{S | SSailors  S.rating > 7 
R(RReserves  R.sid = S.sid
 R.bid = 103)}
{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 boat #103
Find sailors rated > 7 who’ve reserved a red boat
• Observe how the parentheses control the scope of
each quantifier’s binding. (Similar to SQL!)
Division (makes more sense here???)
• 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.
Find sailors who’ve reserved all boats
(hint, use )
{S | SSailors 
BBoats (RReserves
(S.sid = R.sid
 B.bid = R.bid))}
Division – a trickier example…
{S | SSailors 
B  Boats ( B.color = ‘red’ 
R(RReserves  S.sid = R.sid
 B.bid = R.bid))}
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…
a  b is the same as a  b
• If a is true, b must be
true for the implication
to be true. If a is true
and b is false, the
implication 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
Unsafe Queries, Expressive Power
•  syntactically correct calculus queries that have
an infinite number of answers! 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 language (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
|  


















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
• 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))}
{S | SSailors 
B((BBoats) 
R(RReserves  S.sid = R.sid
 B.bid = R.bid))}
... reserved all red boats
• 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))}
{S | SSailors 
B((BBoats)  (B.color  “red”) 
R(RReserves  S.sid = R.sid
 B.bid = R.bid))}

Contenu connexe

Similaire à lecture9Calc.ppt

Similaire à lecture9Calc.ppt (20)

lecture8Alg.ppt
lecture8Alg.pptlecture8Alg.ppt
lecture8Alg.ppt
 
Relational Calculus
Relational CalculusRelational Calculus
Relational Calculus
 
leanCoR: lean Connection-based DL Reasoner
leanCoR: lean Connection-based DL ReasonerleanCoR: lean Connection-based DL Reasoner
leanCoR: lean Connection-based DL Reasoner
 
asal12 sqliii
asal12 sqliiiasal12 sqliii
asal12 sqliii
 
Algebra
AlgebraAlgebra
Algebra
 
CHAPTER 2 DBMS IN EASY WAY BY MILAN PATEL
CHAPTER 2 DBMS IN EASY WAY BY  MILAN PATELCHAPTER 2 DBMS IN EASY WAY BY  MILAN PATEL
CHAPTER 2 DBMS IN EASY WAY BY MILAN PATEL
 
Database managment System Relational Algebra
Database managment System  Relational AlgebraDatabase managment System  Relational Algebra
Database managment System Relational Algebra
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
Transformations
TransformationsTransformations
Transformations
 
Latin square design
Latin square designLatin square design
Latin square design
 
Mesh Shape Editing
Mesh Shape EditingMesh Shape Editing
Mesh Shape Editing
 
Intro To Formula Project
Intro To Formula ProjectIntro To Formula Project
Intro To Formula Project
 
14285 lecture2
14285 lecture214285 lecture2
14285 lecture2
 
Compiler unit 5
Compiler  unit 5Compiler  unit 5
Compiler unit 5
 
Introduction to lambda calculus
Introduction to lambda calculusIntroduction to lambda calculus
Introduction to lambda calculus
 
Dbms1
Dbms1Dbms1
Dbms1
 
Algebra
AlgebraAlgebra
Algebra
 
M1 unit v-jntuworld
M1 unit v-jntuworldM1 unit v-jntuworld
M1 unit v-jntuworld
 
lecture14.ppt
lecture14.pptlecture14.ppt
lecture14.ppt
 
Intro to relational model
Intro to relational modelIntro to relational model
Intro to relational model
 

Plus de rituah

421215833-UNIT-1-OOAD-ppt.ppt
421215833-UNIT-1-OOAD-ppt.ppt421215833-UNIT-1-OOAD-ppt.ppt
421215833-UNIT-1-OOAD-ppt.pptrituah
 
OOAD-Unit1.ppt
OOAD-Unit1.pptOOAD-Unit1.ppt
OOAD-Unit1.pptrituah
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptxrituah
 
421215833-UNIT-1-OOAD-ppt.ppt
421215833-UNIT-1-OOAD-ppt.ppt421215833-UNIT-1-OOAD-ppt.ppt
421215833-UNIT-1-OOAD-ppt.pptrituah
 
Unit2 - Metrics.pptx
Unit2 - Metrics.pptxUnit2 - Metrics.pptx
Unit2 - Metrics.pptxrituah
 
CSBS-TCS.pptx
CSBS-TCS.pptxCSBS-TCS.pptx
CSBS-TCS.pptxrituah
 

Plus de rituah (6)

421215833-UNIT-1-OOAD-ppt.ppt
421215833-UNIT-1-OOAD-ppt.ppt421215833-UNIT-1-OOAD-ppt.ppt
421215833-UNIT-1-OOAD-ppt.ppt
 
OOAD-Unit1.ppt
OOAD-Unit1.pptOOAD-Unit1.ppt
OOAD-Unit1.ppt
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
421215833-UNIT-1-OOAD-ppt.ppt
421215833-UNIT-1-OOAD-ppt.ppt421215833-UNIT-1-OOAD-ppt.ppt
421215833-UNIT-1-OOAD-ppt.ppt
 
Unit2 - Metrics.pptx
Unit2 - Metrics.pptxUnit2 - Metrics.pptx
Unit2 - Metrics.pptx
 
CSBS-TCS.pptx
CSBS-TCS.pptxCSBS-TCS.pptx
CSBS-TCS.pptx
 

Dernier

Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 

Dernier (20)

Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 

lecture9Calc.ppt

  • 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 – where p and q are formulas – where variable R is a tuple variable – where variable R is a tuple variable       , , , , ,    p p q p q , , ) ) ( ( R p R  ) ) ( ( R p R 
  • 5. Free and Bound Variables • The use of quantifiers and 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)}  X  X • 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 names and ages of sailors with rating above 7. {S |S Sailors  S.rating > 7} {S | S1 Sailors(S1.rating > 7  S.sname = S1.sname  S.age = S1.age)} – Modify this query to answer: Find sailors who are older than 18 or have a rating under 9, and are called ‘Bob’. 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. • Find all sailors with rating above 7
  • 7. Find sailors rated > 7 who’ve reserved boat #103 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
  • 8. Joins (continued) {S | SSailors  S.rating > 7  R(RReserves  R.sid = S.sid  R.bid = 103)} {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 boat #103 Find sailors rated > 7 who’ve reserved a red boat • Observe how the parentheses control the scope of each quantifier’s binding. (Similar to SQL!)
  • 9. Division (makes more sense here???) • 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. Find sailors who’ve reserved all boats (hint, use ) {S | SSailors  BBoats (RReserves (S.sid = R.sid  B.bid = R.bid))}
  • 10. Division – a trickier example… {S | SSailors  B  Boats ( B.color = ‘red’  R(RReserves  S.sid = R.sid  B.bid = R.bid))} 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…
  • 11. a  b is the same as a  b • If a is true, b must be true for the implication to be true. If a is true and b is false, the implication 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
  • 12. Unsafe Queries, Expressive Power •  syntactically correct calculus queries that have an infinite number of answers! 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 language (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 |                    
  • 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 • 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))} {S | SSailors  B((BBoats)  R(RReserves  S.sid = R.sid  B.bid = R.bid))}
  • 18. ... reserved all red boats • 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))} {S | SSailors  B((BBoats)  (B.color  “red”)  R(RReserves  S.sid = R.sid  B.bid = R.bid))}