csc 411/511: dbms design dr. nan wangcsc411_l5_relational calculus 1 relational calculus chapter 4...
DESCRIPTION
CSC411_L5_Relational CalculusDr. Nan Wang 3 Relational Calculus Calculus has variables, constants, comparison ops, logical connectives and quantifiers. – TRC(Tuple Relational Calculus): Variables range over (i.e., get bound to) tuples. – DRC (Domain Relational Calculus): Variables range over domain elements (= field values).TRANSCRIPT
![Page 1: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/1.jpg)
CSC 411/511: DBMS Design
Dr. Nan Wang CSC411_L5_Relational Calculus1
Relational CalculusChapter 4 – Part B
![Page 2: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/2.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang2
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.
![Page 3: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/3.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang3
Relational Calculus
• Calculus has variables, constants, comparison ops, logical connectives and quantifiers.– TRC(Tuple Relational Calculus): Variables range over (i.e.,
get bound to) tuples.
– DRC (Domain Relational Calculus): Variables range over domain elements (= field values).
![Page 4: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/4.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang4
Syntax of TRC Queries
4
![Page 5: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/5.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang5
TRC• { T I p(T) }, where T is a tuple variable and p(T)
denotes a formula that describes T;
5
![Page 6: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/6.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang6
TRC-examples• Q12: Find the names and ages of sailors with a
rating above 7.• Q13:Find the sailor name, boat'id, and reservation
date for each reservation.• Q1:Find the names of sailors who have reserved
boat 103.• Q2: Find the narnes of sailors who have reserved a
red boat.• Q7:Find the names of sailors who have reserved at
least two boats• Q9:Find the narnes of sailors who have reserved
all boats.• Q14:Find sailors who have reserved all red boats.
6
![Page 7: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/7.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang7
TRC-examples
7
![Page 8: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/8.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang8
TRC-examples
8
![Page 9: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/9.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang9
Domain Relational Calculus
• Query has the form:
x x xn p x x xn1 2 1 2, ,..., | , ,...,
Answer includes all tuples that make the formula be true.
x x xn1 2, ,...,p x x xn1 2, ,...,
Formula is recursively defined, starting with simple atomic formulas (getting tuples from relations or making comparisons of values), and building bigger and better formulas using the logical connectives.
![Page 10: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/10.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang10
DRC Formulas• Atomic formula:
– , or X op Y, or X op constant– op is one of
• Formula:– an atomic formula, or– , where p and q are formulas, or– , where variable X is free in p(X), or– , where variable X is free in p(X)
• The use of quantifiers and is said to bind X.– A variable that is not bound is free.
x x xn Rname1 2, ,..., , , , , ,
p p q p q, ,X p X( ( ))X p X( ( ))
X X
![Page 11: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/11.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang11
Free and Bound Variables
• The use of quantifiers and in a formula is said to bind X.– A variable that is not bound is free.
• Let us revisit the definition of a query:
X X
x x xn p x x xn1 2 1 2, ,..., | , ,...,
There is an important restriction: the variables x1, ..., xn that appear to the left of `|’ must be the only free variables in the formula p(...).
![Page 12: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/12.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang12
Example: Find all sailors with a rating above 7
• The condition ensures that the domain variables I, N, T and A are bound to fields of the same Sailors tuple.
• The term to the left of `|’ (which should be read as such that) says that every tuple that satisfies T>7 is in the answer.
• Modify this query to answer:– Find sailors who are older than 18 or have a rating under 9,
and are called ‘Joe’.
I N T A I N T A Sailors T, , , | , , ,
7
I N T A Sailors, , ,
I N T A, , ,I N T A, , ,
![Page 13: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/13.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang13
Example: Find sailors rated > 7 who have reserved boat #103
• We have used as a shorthand for
• Note the use of to find a tuple in Reserves that `joins with’ the Sailors tuple under consideration.
I N T A I N T A Sailors T, , , | , , ,
7
Ir Br D Ir Br D serves Ir I Br, , , , Re 103
Ir Br D, , . . . Ir Br D . . .
![Page 14: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/14.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang14
Example: Find sailors rated > 7 who’ve reserved a red boat
• Observe how the parentheses control the scope of each quantifier’s binding.
• This may look cumbersome, but with a good user interface, it is very intuitive. (MS Access, QBE)
I N T A I N T A Sailors T, , , | , , ,
7
Ir Br D Ir Br D serves Ir I, , , , Re
B BN C B BN C Boats B Br C red, , , , ' '
![Page 15: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/15.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang15
Example: Find sailors who’ve reserved all boats
• Find all sailors I such that for each 3-tuple either it is not a tuple in Boats or there is a tuple in Reserves showing that sailor I has reserved it.
I N T A I N T A Sailors, , , | , , ,
B BN C B BN C Boats, , , ,
Ir Br D Ir Br D serves I Ir Br B, , , , Re
B BN C, ,
![Page 16: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/16.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang16
Example: Find sailors who’ve reserved all boats (again!)
• Simpler notation, same query. (Much clearer!)• To find sailors who’ve reserved all red boats:
I N T A I N T A Sailors, , , | , , ,
B BN C Boats, ,
Ir Br D serves I Ir Br B, , Re
C red Ir Br D serves I Ir Br B
' ' , , Re...
..
![Page 17: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/17.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang17
Unsafe Queries, Expressive Power
• It is possible to write syntactically correct calculus queries that have an infinite number of answers! Such queries are called unsafe.– e.g.,
• It is known that 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.
S S Sailors|
![Page 18: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/18.jpg)
CSC411_L5_Relational CalculusDr. Nan Wang18
Summary• Relational calculus is non-operational, and users
define queries in terms of what they want, not in terms of how to compute it. (Declarativeness.)
• Algebra and safe calculus have same expressive power, leading to the notion of relational completeness.
![Page 19: CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B](https://reader031.vdocuments.us/reader031/viewer/2022012914/5a4d1ae37f8b9ab059977ecb/html5/thumbnails/19.jpg)
CSC 411/511: DBMS Design
Dr. Nan Wang CSC411_L5_Relational Calculus19
Questions?