Summary
NULL
in a nutshell:
-
NULL
means unknown. -
Every field or variable that has been created but not initialised is in a
NULL
state. -
To exclude
NULL
s from a domain or column, add “NOT NULL
” after the type name. -
To find out if
A
isNULL
, use “A IS [NOT] NULL
”. -
Assigning
NULL
is done like assigning values: with “A = NULL
” or an insert list. -
To find out if
A
andB
are the same, with the understanding that allNULL
s are the same and different from anything else, use “A IS [NOT] DISTINCT FROM B
” in Firebird 2 and up.In earlier versions the tests are:equalityA = B or A is null and B is null
inequalityA <> B or A is null and B is not null or A is not null and B is null
-
In Firebird 2 and up you can use
NULL
literals in just about every situation where a regular value is also allowed.In practice this mainly gives you a lot more rope to hang yourself. -
Most of the time,
NULL
operands make the entire operation returnNULL
.Noteworthy exceptions are:-
“
NULL or true
” evaluates totrue
; -
“
NULL and false
” evaluates tofalse
.
-
-
The
IN
,ANY
|SOME
andALL
predicates may (but do not always) returnNULL
if either the left-hand side expression or a list/subresult element isNULL
. -
The
[NOT] EXISTS
predicate never returnsNULL
.The[NOT] SINGULAR
predicate never returnsNULL
in Firebird 2.1 and up.It is broken in all previous versions. -
In aggregate functions only non-
NULL
fields are involved in the computation.Exception:COUNT(*)
. -
In ordered sets,
NULL
s are placed…-
1.0: At the bottom;
-
1.5: At the bottom, unless
NULLS FIRST
specified; -
2.0: At the “small end” (top if ascending, bottom if descending), unless overridden by
NULLS FIRST/LAST
.
-
-
If a
WHERE
orHAVING
clause evaluates toNULL
, the row is not included in the result set. -
If the test expression of an
IF
statement isNULL
, theTHEN
block is skipped and theELSE
block executed. -
A
CASE
statement returnsNULL
:-
If the selected result is
NULL
. -
If no matches are found (simple
CASE
) or no conditions aretrue
(searchedCASE
) and there is noELSE
clause.
-
-
In a simple
CASE
statement, “CASE <null_expr>
” does not match “WHEN <null_expr>
”. -
If the test expression of a
WHILE
statement evaluates toNULL
, the loop is not (re)entered. -
A
FOR
statement is not exited whenNULL
s are received.It continues to loop until either all the rows have been processed or it is interrupted by an exception or a loop-breaking PSQL statement. -
In Primary Keys,
NULL
s are never allowed. -
In Unique Keys and Unique Indices,
NULL
s are-
not allowed in Firebird 1.0;
-
allowed (even multiple) in Firebird 1.5 and higher.
-
-
In Foreign Key columns, multiple
NULL
s are allowed. -
If a
CHECK
constraint evaluates toNULL
, the input is-
rejected under Firebird 1.5 and earlier;
-
accepted under Firebird 2.0 and higher.
-
-
SELECT DISTINCT
considers allNULL
s equal: in a single-column select, at most one is returned. -
UDFs sometimes convert
NULL
<=> non-NULL
in a seemingly random manner. -
The
COALESCE
and*NVL
functions can convertNULL
to a value. -
The
NULLIF
family of functions can convert values toNULL
. -
If you add a
NOT NULL
column without a default value to a populated table, all the entries in that column will beNULL
upon creation.Most clients however — including Firebird’sisql
tool — will falsely report them as zeroes (0
for numerical fields,''
for string fields, etc.) -
If you change a column’s datatype to a
NOT NULL
domain, any existingNULL
s in the column will remainNULL
.Again most clients — includingisql
— will show them as zeroes.
Remember, this is how NULL
works in Firebird SQL.There may be (at times subtle) differences with other RDBMSes.