NULL in the Oracle Database


SQL’s NULL causes so much confusion. The basic idea of NULLto represent missing data—is rather simple, but there are some peculiarities. You have to use IS NULL instead of = NULL, for example. But with the Oracle Database, there are even more oddities. On the one hand, because it does not always handle NULL as required by the SQL standard. On the other hand, because it has a very “special” handling of NULL in indexes.

According to the SQL standard, NULL is not a value—it marks the absence of a value. Consequently, no value can be NULL. But the Oracle Database treats an empty string as NULL:

   SELECT     '0 IS NULL???' AS "what is NULL?" FROM dual
    WHERE      0 IS NULL
UNION ALL
   SELECT    '0 is not null' FROM dual
    WHERE     0 IS NOT NULL
UNION ALL
   SELECT ''''' IS NULL???'  FROM dual
    WHERE    '' IS NULL
UNION ALL
   SELECT ''''' is not null' FROM dual 
    WHERE    '' IS NOT NULL;
what is NULL?
--------------
0 is not null
'' IS NULL???

The concept of NULL is used in many programming languages. No matter where you look, an empty string is never NULL. Except, in the Oracle Database. It is, in fact, impossible to store an empty string in a VARCHAR2 field. If you try, it just stores NULL.

To complete the confusion, there is even a case when the Oracle Database treats NULL as empty string:

SELECT dummy
     , dummy || ''
     , dummy || NULL
  FROM dual;

D D D
- - -
X X X

The concatenation of the DUMMY column (always containing 'X') with NULL should return NULL.

This peculiarity is not only strange, but dangerous. But the Oracle Database’s NULL oddity continues with indexing.

Recent Questions at Ask.Use-The-Index-Luke.com

0
votes
1
answer
229
views

query regd the CBO decision

Apr 17 at 10:27 Hulda(suspended)
index-choice optimizer
0
votes
3
answers
2.0k
views

Examples for Function Based Indexes?

Mar 25 at 15:52 Castorp 1
function-based
0
votes
1
answer
610
views

Updating multiple rows using a subquery in SQL

Jan 08 at 09:52 Jan 26
subquery update sql