How do I rep开发者_Go百科resent the SQL "not exists" clause in relational algebra?
The SQL NOT EXISTS
construct can be represented in relational algebra by the antijoin ▹
.
The antijoin L ▹ R
of two relations L
and R
selects those tuples of L
that do not join with any tuple in R
. It can be defined in terms of set difference and left semijoin as follows:
L ▹ R = L - (L ⋉ R).
I think you're looking for the existential quantifier (∃), which you could then negate (~∃).
Response to comment: I don't remember most of my relational algebra, but if I was going to take a stab at it I would guess something along the lines of: σ∃σ(Y)(S). Or possibly π∃π(Y)(S); I don't quite remember if you'd want selection or projection for that.
In my case I solved this issue by rewriting the query,
SELECT *
FROM contactperson
WHERE EXISTS(
SELECT *
FROM person
WHERE contactperson.personId = person.id)
to:
SELECT *
FROM contactperson
WHERE personId = (
SELECT id
FROM person
WHERE contactperson.personId = person.id)
It returns the same result and is easier to rewrite to relational algebra using a join.
精彩评论