Download presentation

Presentation is loading. Please wait.

Published byTriston Openshaw Modified about 1 year ago

1
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Part B

2
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke2 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. DRC: Variables range over domain elements (= field values). Both TRC and DRC are simple subsets of first-order logic. Expressions in the calculus are called formulas. An answer tuple is essentially an assignment of constants to variables that make the formula evaluate to true. We discuss only DRC

3
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke3 Domain Relational Calculus Query has the form: { | p( ) } Answer includes all tuples that make the formula p( ) true. Example: tupleformula

4
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke4 Formula Formula is recursively defined, starting with simple atomic formulas, and building bigger and better formulas using the logical connectives.

5
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke5 DRC Formulas Atomic formula: getting tuples from relations: or making comparisons of values X op Y, or X op constant where op is one of ˂, ˃, =, ≤, ≥, ≠. Formula: recursively defined starting with atomic formulas an atomic formula, or If p and q are formulae, so are ¬p, pΛq, and pVq. If p is a formula with domain variable X, then and are also formulae.

6
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke6 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. Important restriction: The variables x 1, x 2, …, xn that appear to the left of “|” must be the only free variables in the formula p(…), All other variables must be bound using a quantifier.

7
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke7 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.

8
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke8 Find all sailors who are older than 18 or have a rating under 9, and are called ‘joe’ { | Sailors Λ (A>18 V T<9) Λ N=“joe” } Find all sailors Who are older than 18 or have a rating under 9 are called ‘joe’

9
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke9 Find sailors rated > 7 who have reserved boat #103 Find all sailor I such that his/her rating is better than 7, and there exist a reservation … … for boat 103 … and the reservation is for I … Note: Ir, Br, and D are bound

10
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke10 Find sailors rated > 7 who have reserved boat #103 Note the use of Ǝ to find a tuple in Reserves that `joins with’ the Sailors tuple under consideration. Reference Use “Ǝ Ir, Br, D (…)” as a short hand for: “Ǝ Ir ( Ǝ Br ( Ǝ D (…)))”

11
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke11 Find sailors rated > 7 who’ve reserved a red boat ( A … ( B … ( C … ) ) ) BLUE ZONE RED ZONE BLACK ZONE Find a sailor I rated better than 7 There exists a reservation of Br for I Br is a boat and its color is read

12
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke12 Find sailors rated > 7 who’ve reserved a red boat ( A … ( B … ( C … ) ) ) BLUE ZONE RED ZONE BLACK ZONE A can be referenced in all three zones B can be referenced in only two zones

13
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke13 Find sailors rated > 7 who’ve reserved a red boat The parentheses control the scope of each quantifier’s binding. This may look cumbersome, but with a good user interface, it is very intuitive (e.g., MS Access, QBE)

14
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke14 Find sailors who’ve reserved all boats Sailor I has that reservation the reservation is for boat B IrBrD Reserves INTA Sailors BBNC Boats Output There exists a reservation

15
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke15 Find sailors who’ve reserved all boats for each tuple, either it is not a tuple in Boats, or there is a tuple in Reserves showing that sailor I has reserved it. Find all sailors I such that

16
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke16 Find sailors who’ve reserved all boats If ˂B,BN,C> is a boat, then there is a reservation of this boat Br for sailor I A B

17
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke17 Find sailors who’ve reserved all boats (again!) Simpler notation, same query. To find sailors who’ve reserved all red boats:..... This condition is true for all boats There exists a reservation for sailor I If the boat is red then sailor I has reserved it

18
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke18 Logic “Not(A) ˅ B” is equivalent to “A → B” (i.e., “if A then B”) “Color is not red or there exists a boat reservation for I” is equivalent to “if color is red then there exists a boat reservation for I.”.....

19
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke19 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.

20
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke20 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.

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google