Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 - 1 Relational Calculus Queries are open predicate-calculus formulas General form: { | F(x 1, …, x n ) } –Standard set-builder notation (i.e.

Similar presentations


Presentation on theme: "Chapter 5 - 1 Relational Calculus Queries are open predicate-calculus formulas General form: { | F(x 1, …, x n ) } –Standard set-builder notation (i.e."— Presentation transcript:

1 Chapter 5 - 1 Relational Calculus Queries are open predicate-calculus formulas General form: { | F(x 1, …, x n ) } –Standard set-builder notation (i.e. constructs a set of tuples) –x 1, …, x n are free variables –F is a predicate calculus formula Bound variables as well as free variables Built-in predicates, e.g. <, =, … Relation predicates, e.g. r(x, y, 2, z) Standard Boolean connectors, e.g. , , , , … Standard quantifiers:  and 

2 Chapter 5 - 2 Relational Calculus Basic Project-Select-Join Examples Get room information. { | r(x, y,z, w) } Get room number and room name of rooms that cost less than $85 and have 2 beds. { |  z(r(x, y, 2, z)  z < 85) } Get name and address of guests arriving on 10 May. { |  u  v  w(g(u, x, y, z)  s(u, v, 10 May, w)) }

3 Chapter 5 - 3 Relational Calculus Join, Renaming & Union Examples Get name and address of guests who have a reservation for a room whose name is the same as the guest’s name. { |  a  b  c  d  e  f(g(a, x, y, z)  r(b, x, c, d)  s(a, b, e, f)) } Get name and address of guests who have reservations for more than two days or reservations for two-bed rooms. { |  a  b  c  d  e  f( g(a, x, y, z)  s(a, b, c, d)  (d > 2  r(b, e, 2, f))) }

4 Chapter 5 - 4 Relational Calculus Negation Get guest number and name of guests from someplace other than Boston. { |  z  w(g(x, y, z, w)  w  Boston) } Get guest number of guests who do not have a reservation for room 1. The following is not correct. { |  y  z  w(s(x, y, z, w)  y  1) }

5 Chapter 5 - 5 Relational Calculus Negation and Universal Quantification Get guest number of guests who do not have a reservation for room 1. (continuation of example) Find those who do and negate. { |  y  z  w(s(x, y, z, w)  y = 1) } = { |  y  z  w  (s(x, y, z, w)  y = 1) } = { |  y  z  w(  s(x, y, z, w)  y  1) } = { |  y  z  w(s(x, y, z, w)  y  1) } Almost correct, but yields universal complement. Restrict by using relative complement. { |  t  u  v(g(x, t, u, v)   y  z  w(s(x, y, z,w)  y = 1)) }

6 Chapter 5 - 6 Relational Calculus Universal Quantification Get name and address of guests who have reservations for all presidential suites (rooms with two beds). { |  w(g(w, x, y, z)   a  b  c(r(a, b, 2, c)   d  e(s(w, a, d, e)))) }

7 Chapter 5 - 7 Universal Quantification and SQL Queries { |  w(g(w, x, y, z)   a  b  c(r(a, b, 2, c)   d  e(s(w, a, d, e)))) } = { |  w(g(w, x, y, z)   a  b  c(  r(a, b, 2, c)   d  e(s(w, a, d, e)))) } = { |  w(g(w, x, y, z)   a  b  c(r(a, b, 2, c)   d  e(s(w, a, d, e)))) } = select Name, StreetNr, City from Guest g where not exists (select * from Room r where NrBeds = 2 and not exists (select * from Reservation s where g.GuestNr = s.GuestNr and r.RoomNr = s.RoomNr))


Download ppt "Chapter 5 - 1 Relational Calculus Queries are open predicate-calculus formulas General form: { | F(x 1, …, x n ) } –Standard set-builder notation (i.e."

Similar presentations


Ads by Google