Comments on: What is the deal with NULLs? http://thoughts.davisjeff.com/2009/08/02/what-is-the-deal-with-nulls/ Ideas on Databases, Logic, and Language by Jeff Davis Tue, 19 Jun 2012 16:18:51 +0000 hourly 1 http://wordpress.org/?v=3.3.1 By: Mark http://thoughts.davisjeff.com/2009/08/02/what-is-the-deal-with-nulls/comment-page-1/#comment-906 Mark Fri, 10 Sep 2010 18:09:01 +0000 http://davisjeff.wordpress.com/?p=4#comment-906 Thanks for this post and discussion. I think it is important to distinguish between the context of NULLs. Most of the time, NULLs are a problem with math, not the logic per se. So if they appear in a numeric column, certain rules apply, but in a logical sense, others apply, etc. Also, if they are generated by the engine, from say a JOIN where no rows exist, that's another form of them. If anyone is ineterested, please chime in on a SQL Server post I started called The Logic, Mathematics, and Utility of NULLs. Here is the link: http://www.sqlservercentral.com/Forums/Topic970493-374-1.aspx. Thanks for this post and discussion.

I think it is important to distinguish between the context of NULLs. Most of the time, NULLs are a problem with math, not the logic per se. So if they appear in a numeric column, certain rules apply, but in a logical sense, others apply, etc. Also, if they are generated by the engine, from say a JOIN where no rows exist, that’s another form of them.

If anyone is ineterested, please chime in on a SQL Server post I started called The Logic, Mathematics, and Utility of NULLs. Here is the link:
http://www.sqlservercentral.com/Forums/Topic970493-374-1.aspx.

]]>
By: rolfen http://thoughts.davisjeff.com/2009/08/02/what-is-the-deal-with-nulls/comment-page-1/#comment-244 rolfen Wed, 11 Aug 2010 01:38:19 +0000 http://davisjeff.wordpress.com/?p=4#comment-244 You should waste your brainpower on something else then SQL. There is no logic and elegance to it, and I predict it will soon be superceded by something simpler. You should waste your brainpower on something else then SQL. There is no logic and elegance to it, and I predict it will soon be superceded by something simpler.

]]>
By: Xavier http://thoughts.davisjeff.com/2009/08/02/what-is-the-deal-with-nulls/comment-page-1/#comment-230 Xavier Thu, 01 Jul 2010 08:44:14 +0000 http://davisjeff.wordpress.com/?p=4#comment-230 > SQL’s approach to missing information is, well, “unique”. Not so unique. You might be interested in the way R handles NULL value. It is fairly similar (even though many examples are not applicable). For instance: > sum(NULL, 3) [1] 3 > NULL + 3 numeric(0) > SQL’s approach to missing information is, well, “unique”.

Not so unique. You might be interested in the way R handles NULL value. It is fairly similar (even though many examples are not applicable). For instance:

> sum(NULL, 3)
[1] 3
> NULL + 3
numeric(0)

]]>
By: Darren Duncan http://thoughts.davisjeff.com/2009/08/02/what-is-the-deal-with-nulls/comment-page-1/#comment-200 Darren Duncan Mon, 15 Mar 2010 04:01:04 +0000 http://davisjeff.wordpress.com/?p=4#comment-200 Tim, thanks for the lip-service to my project. It also gives more support to what I'm doing when I hear that others have wanted or tried to do similar, as it shows more that demand is there. Tim, thanks for the lip-service to my project. It also gives more support to what I’m doing when I hear that others have wanted or tried to do similar, as it shows more that demand is there.

]]>
By: Tim Bunce http://thoughts.davisjeff.com/2009/08/02/what-is-the-deal-with-nulls/comment-page-1/#comment-163 Tim Bunce Mon, 07 Dec 2009 21:46:55 +0000 http://davisjeff.wordpress.com/?p=4#comment-163 FYI Darren Duncan has been working on just such a project for several years. http://pugs.postgresql.org/node/404 http://www.mail-archive.com/dbi-users@perl.org/msg31943.html FYI Darren Duncan has been working on just such a project for several years.
http://pugs.postgresql.org/node/404
http://www.mail-archive.com/dbi-users@perl.org/msg31943.html

]]>
By: Jeff Davis http://thoughts.davisjeff.com/2009/08/02/what-is-the-deal-with-nulls/comment-page-1/#comment-96 Jeff Davis Sat, 15 Aug 2009 06:32:30 +0000 http://davisjeff.wordpress.com/?p=4#comment-96 > NULL is the empty set. > > Set operations can appear to be very similar to “normal” mathematical operations, but since they may be missing key properties of that operation (say closure), they act in what appears to be a non-intuitive fashion. What set operations lack closure? As far as I know set operations are a well-defined mathematical system, and the empty set is just a value in that system. Sets are closed over the operations UNION, INTERSECT, DIFFERENCE, etc. Set operations are nothing like NULL semantics, which involve 3VL and strange exceptions. You certainly can't explain all of my examples by just saying "NULL is the empty set". > {NULL} != NULL (i.e. the set containing NULL != NULL alone) If you do "SELECT * FROM foo WHERE NULL = NULL", which NULL is "{NULL}" and which NULL is "NULL"? They look indistinguishable to me, but the predicate does not evaluate to TRUE. > Does that help anyone? I think that this line of reasoning will lead to mistakes. For instance, how do you explain the fact that COUNT(*) doesn't ignore NULLs? Or that "x IS NOT NULL" is not the same as "NOT x IS NULL" for all x? > NULL is the empty set.
>
> Set operations can appear to be very similar to “normal” mathematical operations, but since they may be missing key properties of that operation (say closure), they act in what appears to be a non-intuitive fashion.

What set operations lack closure? As far as I know set operations are a well-defined mathematical system, and the empty set is just a value in that system. Sets are closed over the operations UNION, INTERSECT, DIFFERENCE, etc.

Set operations are nothing like NULL semantics, which involve 3VL and strange exceptions. You certainly can’t explain all of my examples by just saying “NULL is the empty set”.

> {NULL} != NULL (i.e. the set containing NULL != NULL alone)

If you do “SELECT * FROM foo WHERE NULL = NULL”, which NULL is “{NULL}” and which NULL is “NULL”? They look indistinguishable to me, but the predicate does not evaluate to TRUE.

> Does that help anyone?

I think that this line of reasoning will lead to mistakes. For instance, how do you explain the fact that COUNT(*) doesn’t ignore NULLs? Or that “x IS NOT NULL” is not the same as “NOT x IS NULL” for all x?

]]>
By: Ken http://thoughts.davisjeff.com/2009/08/02/what-is-the-deal-with-nulls/comment-page-1/#comment-95 Ken Fri, 14 Aug 2009 16:48:19 +0000 http://davisjeff.wordpress.com/?p=4#comment-95 Why is it that no one seems to know that ... NULL is the empty set. All operations in SQL are based on set theory, not algebra as we normally consider it. Set theory is the *basis* for (ordinary) mathematics and mathematical operations. Set operations can appear to be very similar to "normal" mathematical operations, but since they may be missing key properties of that operation (say closure), they act in what appears to be a non-intuitive fashion. So the definition of NULL is always consistent, it is the definition of operations over a set which may treat NULL elements in the set differently. What is often the case is that what appears to be a case of ... NULL == NULL is really a case of ... {NULL} != NULL (i.e. the set containing NULL != NULL alone) For example, Aggregates often appear to be mathematical operations, like SUM(), but it is really defined as the operation over a set of elements which are either a number or elements convertible to a number. And when converting a NULL to a number for the purposes of a "SUM", it is easy to say that its number value is zero (which is the same as ignoring it). Otherwise, SUM would be largely useless as an operation. As such the SUM *operator* is not "+" it is a different (albeit similar) operator, and it inherently ignores NULL elements in the set in its computation. It isn't inconsistent with "+", because it was never meant to be the same as "+". Does that help anyone? Why is it that no one seems to know that …

NULL is the empty set.

All operations in SQL are based on set theory, not algebra as we normally consider it. Set theory is the *basis* for (ordinary) mathematics and mathematical operations. Set operations can appear to be very similar to “normal” mathematical operations, but since they may be missing key properties of that operation (say closure), they act in what appears to be a non-intuitive fashion.

So the definition of NULL is always consistent, it is the definition of operations over a set which may treat NULL elements in the set differently.

What is often the case is that what appears to be a case of …
NULL == NULL
is really a case of …
{NULL} != NULL (i.e. the set containing NULL != NULL alone)

For example, Aggregates often appear to be mathematical operations, like SUM(), but it is really defined as the operation over a set of elements which are either a number or elements convertible to a number. And when converting a NULL to a number for the purposes of a “SUM”, it is easy to say that its number value is zero (which is the same as ignoring it). Otherwise, SUM would be largely useless as an operation.

As such the SUM *operator* is not “+” it is a different (albeit similar) operator, and it inherently ignores NULL elements in the set in its computation. It isn’t inconsistent with “+”, because it was never meant to be the same as “+”.

Does that help anyone?

]]>
By: Four short links: 6 August 2009 | Design Website http://thoughts.davisjeff.com/2009/08/02/what-is-the-deal-with-nulls/comment-page-1/#comment-92 Four short links: 6 August 2009 | Design Website Mon, 10 Aug 2009 10:56:57 +0000 http://davisjeff.wordpress.com/?p=4#comment-92 [...] What is the Deal with NULLs? — In the past, I’ve criticized NULL semantics, but in this post I’d just like to explain some corner cases that I think you’ll find interesting, and try to straighten out some myths and misconceptions. [...] I believe the above shows, beyond a reasonable doubt, that NULL semantics are unintuitive, and if viewed according to most of the “standard explanations,” highly inconsistent. (via bos on Delicious) [...] [...] What is the Deal with NULLs? — In the past, I’ve criticized NULL semantics, but in this post I’d just like to explain some corner cases that I think you’ll find interesting, and try to straighten out some myths and misconceptions. [...] I believe the above shows, beyond a reasonable doubt, that NULL semantics are unintuitive, and if viewed according to most of the “standard explanations,” highly inconsistent. (via bos on Delicious) [...]

]]>
By: Jeff Davis http://thoughts.davisjeff.com/2009/08/02/what-is-the-deal-with-nulls/comment-page-1/#comment-90 Jeff Davis Sun, 09 Aug 2009 18:05:57 +0000 http://davisjeff.wordpress.com/?p=4#comment-90 > #1 – Always add a not null constraint on all your fields when creating a table. Unfortunately, I don't think that's a universal solution. (a) NULLs will be generated anyway, by outer join and aggregates; and (b) most SQL implementations don't effectively optimize physical designs that avoid NULLs, so there may be a significant performance penalty. > #3 – When you have a nullable field, go all over your client code and make sure you coalesce your comparisons, your strings concatenation, etc. That's certainly good practice. When you have a query, look at any fields that can possibly be NULL, consider how those might affect your application differently, and handle them appropriately. > #5 – Never use null as a meaningful value. e.g. if you want to store the date value “infinite time in the past”, use the lowest date available in the system, not “null value”. I think that's a good rule. Don't use NULL as an arbitrary "special value" > #1 – Always add a not null constraint on all your fields when creating a table.

Unfortunately, I don’t think that’s a universal solution. (a) NULLs will be generated anyway, by outer join and aggregates; and (b) most SQL implementations don’t effectively optimize physical designs that avoid NULLs, so there may be a significant performance penalty.

> #3 – When you have a nullable field, go all over your client code and make sure you coalesce your comparisons, your strings concatenation, etc.

That’s certainly good practice. When you have a query, look at any fields that can possibly be NULL, consider how those might affect your application differently, and handle them appropriately.

> #5 – Never use null as a meaningful value. e.g. if you want to store the date value “infinite time in the past”, use the lowest date available in the system, not “null value”.

I think that’s a good rule. Don’t use NULL as an arbitrary “special value”

]]>
By: IT Security Blog» Blog Archive » delicious/cdman83 http://thoughts.davisjeff.com/2009/08/02/what-is-the-deal-with-nulls/comment-page-1/#comment-87 IT Security Blog» Blog Archive » delicious/cdman83 Fri, 07 Aug 2009 15:49:00 +0000 http://davisjeff.wordpress.com/?p=4#comment-87 [...] What is the deal with NULLs? [...] [...] What is the deal with NULLs? [...]

]]>