Summary
<< Testing for NULL and equality in practice | Firebird Null Guide | A: NULL-related bugs in Firebird >>
Summary
NULL in a nutshell:
NULLmeans unknown.- To exclude
NULLs from a domain or column, addNOT NULLafter the type name. - To find out if
AisNULL, useA IS [NOT] NULL. - Assigning
NULLis done like assigning values: withA = NULLor an insert list. - To find out if
AandBare the same, with the understanding that allNULLs are the same and different from anything else, useA IS [NOT] DISTINCT FROM Bin Firebird 2 and up. In earlier versions the tests are:
// equality:
A = B or A is null and B is null
// inequality:
A <> 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 trueevaluates totrueNULL 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 are true (searchedCASE) and there is noELSEclause.
- If the selected result is
- In a simple
CASEstatement,CASE <null_expr>does not matchWHEN <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.
back to top of page
<< Testing for NULL and equality in practice | Firebird Null Guide | A: NULL-related bugs in Firebird >>







