Summary
NULL in a nutshell:
-
NULLmeans unknown. -
Every field or variable that has been created but not initialised is in a
NULLstate. -
To exclude
NULLs from a domain or column, add “NOT NULL” after the type name. -
To find out if
AisNULL, use “A IS [NOT] NULL”. -
Assigning
NULLis done like assigning values: with “A = NULL” or an insert list. -
To find out if
AandBare the same, with the understanding that allNULLs 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 nullinequalityA <> 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
NULLliterals 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,
NULLoperands make the entire operation returnNULL.Noteworthy exceptions are:-
“
NULL or true” evaluates totrue; -
“
NULL and false” evaluates tofalse.
-
-
The
IN,ANY|SOMEandALLpredicates may (but do not always) returnNULLif either the left-hand side expression or a list/subresult element isNULL. -
The
[NOT] EXISTSpredicate never returnsNULL.The[NOT] SINGULARpredicate never returnsNULLin Firebird 2.1 and up.It is broken in all previous versions. -
In aggregate functions only non-
NULLfields are involved in the computation.Exception:COUNT(*). -
In ordered sets,
NULLs are placed…-
1.0: At the bottom;
-
1.5: At the bottom, unless
NULLS FIRSTspecified; -
2.0: At the “small end” (top if ascending, bottom if descending), unless overridden by
NULLS FIRST/LAST.
-
-
If a
WHEREorHAVINGclause evaluates toNULL, the row is not included in the result set. -
If the test expression of an
IFstatement isNULL, theTHENblock is skipped and theELSEblock executed. -
A
CASEstatement returnsNULL:-
If the selected result is
NULL. -
If no matches are found (simple
CASE) or no conditions aretrue(searchedCASE) and there is noELSEclause.
-
-
In a simple
CASEstatement, “CASE <null_expr>” does not match “WHEN <null_expr>”. -
If the test expression of a
WHILEstatement evaluates toNULL, the loop is not (re)entered. -
A
FORstatement is not exited whenNULLs 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,
NULLs are never allowed. -
In Unique Keys and Unique Indices,
NULLs are-
not allowed in Firebird 1.0;
-
allowed (even multiple) in Firebird 1.5 and higher.
-
-
In Foreign Key columns, multiple
NULLs are allowed. -
If a
CHECKconstraint evaluates toNULL, the input is-
rejected under Firebird 1.5 and earlier;
-
accepted under Firebird 2.0 and higher.
-
-
SELECT DISTINCTconsiders allNULLs equal: in a single-column select, at most one is returned. -
UDFs sometimes convert
NULL<=> non-NULLin a seemingly random manner. -
The
COALESCEand*NVLfunctions can convertNULLto a value. -
The
NULLIFfamily of functions can convert values toNULL. -
If you add a
NOT NULLcolumn without a default value to a populated table, all the entries in that column will beNULLupon creation.Most clients however — including Firebird’sisqltool — will falsely report them as zeroes (0for numerical fields,''for string fields, etc.) -
If you change a column’s datatype to a
NOT NULLdomain, any existingNULLs 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.