Why does Oracle 9i treat an empty string as NULL?

后端 未结 10 1402
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 23:39

I know that it does consider \' \' as NULL, but that doesn\'t do much to tell me why this is the case. As I understand the SQL specifications

相关标签:
10条回答
  • 2020-11-22 00:11

    According to official 11g docs

    Oracle Database currently treats a character value with a length of zero as null. However, this may not continue to be true in future releases, and Oracle recommends that you do not treat empty strings the same as nulls.

    Possible reasons

    1. val IS NOT NULL is more readable than val != ''
    2. No need to check both conditions val != '' and val IS NOT NULL
    0 讨论(0)
  • 2020-11-22 00:13

    Indeed, I have had nothing but difficulties in dealing with Oracle, including invalid datetime values (cannot be printed, converted or anything, just looked at with the DUMP() function) which are allowed to be inserted into the database, apparently through some buggy version of the client as a binary column! So much for protecting database integrity!

    Oracle handling of NULLs links:

    http://digitalbush.com/2007/10/27/oracle-9i-null-behavior/

    http://jeffkemponoracle.com/2006/02/empty-string-andor-null.html

    0 讨论(0)
  • 2020-11-22 00:21

    Because not treating it as NULL isn't particularly helpful, either.

    If you make a mistake in this area on Oracle, you usually notice right away. In SQL server, however, it will appear to work, and the problem only appears when someone enters an empty string instead of NULL (perhaps from a .net client library, where null is different from "", but you usually treat them the same).

    I'm not saying Oracle is right, but it seems to me that both ways are approximately equally bad.

    0 讨论(0)
  • 2020-11-22 00:21

    First of all, null and null string were not always treated as the same by Oracle. A null string is, by definition, a string containing no characters. This is not at all the same as a null. NULL is, by definition, the absence of data.

    Five or six years or so ago, null string was treated differently from null by Oracle. While, like null, null string was equal to everything and different from everything (which I think is fine for null, but totally WRONG for null string), at least length(null string) would return 0, as it should since null string is a string of zero length.

    Currently in Oracle, length(null) returns null which I guess is O.K., but length(null string) also returns null which is totally WRONG.

    I do not understand why they decided to start treating these 2 distinct "values" the same. They mean different things and the programmer should have the capability of acting on each in different ways. The fact that they have changed their methodology tells me that they really don't have a clue as to how these values should be treated.

    0 讨论(0)
提交回复
热议问题