What is the relational algebra expression of the query below? I couldn't find the expression for "Is Null".
SELECT reader.name
FROM reader LEFT JOIN book_borrow ON reader.cardid = book_borrow.cardid
WHERE book_borrow.cardid Is Null;
Returns a Boolean value that indicates whether an expression contains no valid data (Null). The required expressionargument is a Variant containing a numeric expression or string expression. IsNull returns True if expression is Null; otherwise, IsNull returns False.
The value 0 (all bits at zero) is a typical value used in memory to denote null . It means that there is no value associated with name . You can also think of it as the absence of data or simply no data.
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).
A null value in a relational database is used when the value in a column is unknown or missing. A null is neither an empty string (for character or datetime data types) nor a zero value (for numeric data types).
This task requires a bit of creativity, not a verbatim translation.
What's happening here in this query? First we left join book_borrow on reader. Remember the definition: even if the ON clause matches no rows in the rightmost table, the join will still return a single row such that it contains NULLs in the fields of the right table. Our table looks like this:
reader.name | reader.cardid | book_borrow.cardid | book_borrow.book_id
Alice | 1 | 1 | 1
Alice | 1 | 1 | 5
Bob | 2 | 2 | 5
Charlie | 3 | NULL | NULL
We can see that Alice has borrowed two books (of ids 1 and 5), Bob borrowed one (id 5), and Charlie got NULL in his book_borrow fields because he has borrowed none. The query then proceeds to fetch only the rows where book_borrow.cardid is NULL, so the query just says: "get all the people who haven't borrowed any books".
Phrasing the task like this, writing the relational algebra expression is easy:
Here it is, unleashing my Latex:
The morals of the story: even though, as Erwin points out, there's no null in the purest form of relational algebra (since relational algebra builds on first-order logic), we don't always need it to express lack of something; left joins can be expressed by basic operators; left joins were invented to improve the efficiency of the calculation: you can easily see how taking the left join and selecting nulls is a lot more practical.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With