Oracle中NULL值的好处是什么?

时间:2016-05-11 17:41:57

标签: oracle oracle11g null

我正在尝试理解Oracle如何处理NULL值。这更像是要求我确认我已经发现的内容,以确保我没有忽略一些明显的东西。下面是确认Oracle处理NULL值的脚本与处理空字符串相同。如果是这种情况,那么祈祷告诉为什么要开发任何Oracle数据库以允许字段为NULL,而不是在查询时将混淆引入索引(即...... WHERE NVL(FieldName,'默认值) ')!='测试')???

-- Returns only 2 rows
-- Oracle doesn't differentiate from blank strings and NULL values
SELECT NULL AS Test_Nulls from dual
UNION
SELECT '' AS Test_Nulls from dual
UNION
SELECT 'Test' AS Test_Nulls from dual;

-- Returns "X"
-- Further proof Oracle doesn't differentiate from blank strings and NULL values
SELECT 'X' AS Test_Nulls
FROM dual
WHERE '' is NULL;

-- Returns "X"
-- Even further proof Oracle doesn't differentiate from blank strings and NULL values
SELECT NVL('','X') AS Test_Nulls
FROM dual;

-- Returns 3 rows
-- Oracle recognizes <Carriage Return> + <Line Feed>
SELECT '' AS Returned from dual
UNION
SELECT '
' AS Returned FROM dual
UNION
SELECT 'Test' AS Returned FROM dual;

1 个答案:

答案 0 :(得分:0)

简单回答:对于我在Oracle中进行的任何新的数据库设计工作,我将把所有varchar2和nvarchar2以及char2和nchar2字段设置为不允许NULL并且默认值为空字符串。我一直怀疑添加“2”后缀的命名约定,以及它不是其他数据库平台的常见字段类型这一事实。对我而言,似乎是最后一刻绝望意味着甲骨文在早期处理了一种情况,比如“噢,好吧......我们不能做对,所以我们只是给它一个新名字!”这是我在这件事上的2美分。