I'm trying to convert an sql query into tuple relational calculus, but there is a NOT EXIST
that causes me to be stuck.
SQL query:
SELECT num FROM a a1, b b1
WHERE a1.num = b1.no AND a1.name = "Tim"
AND NOT EXIST
(SELECT * FROM a a2, b b2
WHERE a2.num = b2.no AND a2.name = "Tim"
AND b2.rating > b1.rating)
I already started with:
{ t: num | ∃a1 ∈ a ∃b1 ∈ b [ t(num) = a1(num) ∧ a1.name = "Tim" ∧ ¬∃a2 ∈ a ...
this is where I'm stuck. How do I show that it is NOT EXISTS
from both a2
and b2
? If I write ¬∃a2 ∈ a ¬∃b2 ∈ b
then this is going to be a doubly nested sql query and not the one listed above.
You want there to not exist
a2
andb2
per theWHERE
.SQL expression
corresponds to tuple calculus expression
In SQL there
EXISTS
a row in the subquery if and only if there exista2
andb2
row values per itsWHERE
to form that row. SoNOT EXISTS
such an SQLa2
plusb2
pair when it's not the case that there are calculusa2
andb2
where...
.