Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

relational algebra expression for "is null"

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;
like image 705
Xtrageik Avatar asked Dec 09 '13 21:12

Xtrageik


People also ask

Is NULL in expression?

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.

How do you represent a null value?

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.

How do you write not exist 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).

What are NULL values in relation?

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).


1 Answers

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:

  • let's make a natural join of reader and book_borrow, that will yield us the unwanted rows, names of the people who borrowed any book.
  • then just subtract these people from the set of all people, and the result is the set of people who haven't borrowed a book.

Here it is, unleashing my Latex:

π(name){ π(name, carddid){Reader} - π(name, cardid){Reader join Book_borrow} }

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.

like image 132
SáT Avatar answered Sep 29 '22 14:09

SáT