Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Relational Calculus (TRC)

“All” and “Only” Queries


Chapter 4

1
Example: Find sailors who’ve
reserved all red boats (1)

Sailors for which


Sailors who
there exists a red
have reserved
boat they have
all red boats
not reserved

The complement of
The answer set
the answer set

 If we can’t (easily) define the answer set, it


sometimes helps to negate the definition of the
complement of the answer set!
 For example: “All” and “Only” queries
2
Find sailors who’ve reserved all red
boats (2)
Sailors for which
Sailors who
there exists a red
have reserved
boat they have
all red boats
not reserved

 The negation of the complement of the answer set:


 Find all sailors S such it is not true that there exists a red
boat B which they have not reserved.

{S | S ∈ Sailors ∧ ¬(∃B ∈ Boats (B.color=“red” ∧


¬(∃R ∈ Reserves (R.sid = S.sid ∧ R.bid=B.bid))))}
3
Find sailors who’ve reserved all red
boats (3)

{S | S ∈ Sailors ∧ ∀B ∈ Boats ¬(B.color=“red” ∧


¬(∃R ∈ Reserves (R.sid = S.sid ∧ R.bid=B.bid)))}

 Find all sailors S such that for every boat in B: it is


not true that it is both red and there is not
reservation for it by S.
 Applying: ¬ (∃x(P(x))) ⇔ ∀x(¬P(x))

4
Find sailors who’ve reserved all red
boats (4)

{S | S ∈ Sailors ∧ ∀B ∈ Boats (¬B.color=“red” ∨


(∃R ∈ Reserves (R.sid = S.sid ∧ R.bid=B.bid)))}

 Find all sailors S such that for every boat in B: either


it is not red, or there is a reservation for it by S.
 Applying: ¬(P∧Q) ⇔ ¬ P ∨¬ Q

5
Find sailors who’ve reserved all red
boats (5)

{S | S ∈ Sailors ∧ ∀B ∈ Boats (B.color=“red”) ⇒


(∃R ∈ Reserves (R.sid = S.sid ∧ R.bid=B.bid))}

 Find all sailors S such that for every boat in B: if it is


red, there must be reservation for it by S.
 Applying: ¬P ∨ Q ⇔ P⇒ Q

6
Wrong way: Find sailors who’ve
reserved all red boats (6)

{S | S ∈ Sailors ∧ ∀B ∈ Boats ((B.color=“red”) ∧


(∃R ∈ Reserves (R.sid = S.sid ∧ R.bid=B.bid)))}

 What is wrong with this?


 Only works if a) there no boats at all, or b) all boats
are red.

7
Easier: Find sailors who’ve reserved
all boats

{S | S ∈ Sailors ∧ ∀B ∈ Boats
(∃R ∈ Reserves (R.sid = S.sid ∧ R.bid=B.bid))}

 Find all sailors S such that for every boat in B there


must be reservation for it by S.

8
Find sailors who’ve reserved only red
boats (1)
Sailors for which
Sailors who
there exists a non-
have reserved
red boat they have
only red boats
reserved

 The negation of the complement of the answer set:


 Find all sailors S such it is not true that there exists a non-
red boat B which they have reserved.

{S | S ∈ Sailors ∧ ¬(∃B ∈ Boats (B.color≠“red” ∧


(∃R ∈ Reserves (R.sid = S.sid ∧ R.bid=B.bid))))}
9
Find sailors who’ve reserved only red
boats (2)

{S | S ∈ Sailors ∧ (∀B ∈ Boats ¬(B.color≠“red” ∧


(∃R ∈ Reserves (R.sid = S.sid ∧ R.bid=B.bid))))}

 Find all sailors S such that for every boat in B: it is


not true that B is not red and S reserved it.
 Applying: ¬ (∃x(P(x))) ⇔ ∀x(¬P(x))

10
Find sailors who’ve reserved only red
boats (3)

{S | S ∈ Sailors ∧ (∀B ∈ Boats (B.color =“red” ∨


¬(∃R ∈ Reserves (R.sid = S.sid ∧ R.bid=B.bid))))}

 Find all sailors S such that for every boat in B: either


B is red or S did not reserve it.
 Applying: ¬(P∧Q) ⇔ ¬ P ∨¬ Q

11
Find sailors who’ve reserved only red
boats (4)

{S | S ∈ Sailors ∧ (∀B ∈ Boats (B.color ≠ “red”) ⇒


¬(∃R ∈ Reserves (R.sid = S.sid ∧ R.bid=B.bid)))}

 Find all sailors S such that for every boat in B: if B is


not red, then S did not reserve it.
 Applying: ¬P ∨ Q ⇔ P⇒ Q

12
Find sailors who’ve reserved only red
boats (5)

{S | S ∈ Sailors ∧ (∀B ∈ Boats (B.color ≠ “red”) ⇒


¬(∃R ∈ Reserves (R.sid = S.sid ∧ R.bid=B.bid)))}
 Find all sailors S such that for every boat in B: if B is
not red, then S did not reserve it.
 Questions to ponder:
 What if S has no reservations?
 What if there are no boats?
 What if all boats are red?
 What if all boats are not red?
13

You might also like