Null Values
Null Values
SQL allows the use of null values to indicate absence of information about the value of an attribute.
We can use the special keyword null in a predicate to test for a null value. Thus, to find all loan numbers that appear in the loan relation with null values for amount, we write
select loan-number
from loan
where amount is null
The predicate is not null tests for the absence of a null value.
The use of a null value in arithmetic and comparison operations causes several complications. In Section 3.3.4 we saw how null values are handled in the relational algebra. We now outline how SQL handles null values.
The result of an arithmetic expression (involving, for example +, −, ∗ or /) is null if any of the input values is null. SQL treats as unknown the result of any comparison involving a null value (other than is null and is not null).
Since the predicate in a where clause can involve Boolean operations such as and, or, and not on the results of comparisons, the definitions of the Boolean operations are extended to deal with the value unknown, as outlined in Section 3.3.4.
• and: The result of true and unknown is unknown, false and unknown is false, while unknown and unknown is unknown.
• or: The result of true or unknown is true, false or unknown is unknown, while unknown or unknown is unknown.
• not: The result of not unknown is unknown.
SQL defines the result of an SQL statement of the form
select ... from R1, ··· , Rn where P
to contain (projections of) tuples in R1 × ··· × Rn for which predicate P evaluates to true. If the predicate evaluates to either false or unknown for a tuple in R1 × ··· × Rn (the projection of) the tuple is not added to the result.
SQL also allows us to test whether the result of a comparison is unknown, rather than true or false, by using the clauses is unknown and is not unknown.
Null values, when they exist, also complicate the processing of aggregate operators. For example, assume that some tuples in the loan relation have a null value for amount. Consider the following query to total all loan amounts:
select sum (amount)
from loan
The values to be summed in the preceding query include null values, since some tuples have a null value for amount. Rather than say that the overall sum is itself null, the SQL standard says that the sum operator should ignore null values in its input.
In general, aggregate functions treat nulls according to the following rule: All aggregate functions except count(*) ignore null values in their input collection. As a result of null values being ignored, the collection of values may be empty. The count of an empty collection is defined to be 0, and all other aggregate operations return a value of null when applied on an empty collection. The effect of null values on some of the more complicated SQL constructs can be subtle.
A boolean type data, which can take values true, false, and unknown, was introduced in SQL:1999. The aggregate functions some and every, which mean exactly what you would intuitively expect, can be applied on a collection of Boolean values.
Comments
Post a Comment