我知道它确实认为' '为NULL,但这并没有告诉我为什么会这样。根据我对SQL规范的理解,' '与NULL不一样——一个是有效的数据,另一个是表明没有相同的信息。
请随意猜测,但如果是这样,请指出来。如果甲骨文有谁能对此发表评论,那就太棒了!
我知道它确实认为' '为NULL,但这并没有告诉我为什么会这样。根据我对SQL规范的理解,' '与NULL不一样——一个是有效的数据,另一个是表明没有相同的信息。
请随意猜测,但如果是这样,请指出来。如果甲骨文有谁能对此发表评论,那就太棒了!
当前回答
甲骨文公司副总裁汤姆·凯特:
长度为0的varchar被视为 NULL。 不被视为NULL。 当赋值给char(1)时变成 ' '(字符类型是空白填充的 字符串)。 当分配给varchar2(1)时 变成",长度为零 字符串和零长度的字符串是 Oracle中的NULL(它不是长”)
其他回答
甲骨文公司副总裁汤姆·凯特:
长度为0的varchar被视为 NULL。 不被视为NULL。 当赋值给char(1)时变成 ' '(字符类型是空白填充的 字符串)。 当分配给varchar2(1)时 变成",长度为零 字符串和零长度的字符串是 Oracle中的NULL(它不是长”)
我相信答案是甲骨文非常非常古老。
早在SQL标准出现之前,Oracle的设计决定是VARCHAR/VARCHAR2列中的空字符串为NULL,并且只有一种意义上的NULL(有关系理论家会区分从未提示的数据、存在答案但用户不知道的数据、没有答案的数据等等,所有这些都构成了某种意义上的NULL)。
By the time that the SQL standard came around and agreed that NULL and the empty string were distinct entities, there were already Oracle users that had code that assumed the two were equivalent. So Oracle was basically left with the options of breaking existing code, violating the SQL standard, or introducing some sort of initialization parameter that would change the functionality of potentially large number of queries. Violating the SQL standard (IMHO) was the least disruptive of these three options.
Oracle保留了在未来的版本中更改VARCHAR数据类型以遵循SQL标准的可能性(这就是为什么每个人都在Oracle中使用VARCHAR2,因为数据类型的行为保证在未来保持不变)。
书中的例子
set serveroutput on;
DECLARE
empty_varchar2 VARCHAR2(10) := '';
empty_char CHAR(10) := '';
BEGIN
IF empty_varchar2 IS NULL THEN
DBMS_OUTPUT.PUT_LINE('empty_varchar2 is NULL');
END IF;
IF '' IS NULL THEN
DBMS_OUTPUT.PUT_LINE(''''' is NULL');
END IF;
IF empty_char IS NULL THEN
DBMS_OUTPUT.PUT_LINE('empty_char is NULL');
ELSIF empty_char IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('empty_char is NOT NULL');
END IF;
END;
事实上,我在处理Oracle时遇到了困难,包括无效的datetime值(不能打印,转换或任何东西,只是用DUMP()函数查看),允许将其插入到数据库中,显然是通过一些有bug的客户端版本作为二进制列!保护数据库完整性就说这么多!
Oracle对null链接的处理:
http://digitalbush.com/2007/10/27/oracle-9i-null-behavior/
http://jeffkemponoracle.com/2006/02/empty-string-andor-null.html
I suspect this makes a lot more sense if you think of Oracle the way earlier developers probably did -- as a glorified backend for a data entry system. Every field in the database corresponded to a field in a form that a data entry operator saw on his screen. If the operator didn't type anything into a field, whether that's "birthdate" or "address" then the data for that field is "unknown". There's no way for an operator to indicate that someone's address is really an empty string, and that doesn't really make much sense anyways.