Sunday, September 5, 2010

NULLs are not the same in SQL and Java

In SQL, NULL really means "unknown". Of course, when you compare one value that is "unknown" to another value that is also "unknown", the result can only be "unknown".
In C# (and Java, etc), null doesn't mean "unknown". It means "there is no object here". The correct way to translate that to relational terms would be to treat every object reference as a reference to a relation which may have either 0 or 1 tuples. Null means 0 tuples. Obviously, two relations with 0 tuples each are definitely equal - "unknown" doesn't enter into equation here.