也来说说SQL语句中NULL的真实含义
2009-01-23 15:05
316 查看
NULL,表示不明确、未知的列值
测试表:testnull(id varchar(32))
数据库:Sybase ASA11.0
行数据(''), (NULL)
数据库选项ansinull为true(也是ASA数据库的默认选项)时,
select * from testnull where id = null
select * from testnull where id != null
结果均为空
select * from testnull where id is null
结果为(NULL)
select * from testnull where id is not null
结果为('')
当ansinull为false时,
select * from testnull where id = null
结果为(NULL)
select * from testnull where id != null
结果为('')
从上述结果来看,NULL值确实是一个有争议的东西,但是,毫无疑问,ansinull对NULL的定义是精确的,即不能对NULL值进行等于或不等判断,无论是等还是不等,其结果都为false.
而统一的is null, is not null的含义则显然是明确的,NULL is null恒为真,非NULL is null恒为假。
再看看在Oracle中的结果:
SQL> select * from testnull where id is null;
ID
--------------------------------
SQL> select * from testnull where id is not null;
no rows selected
SQL> select * from testnull where id=null;
no rows selected
SQL> select * from testnull where id != null;
no rows selected
空字符串''在oracle中被示为NULL值了。比较怪异。
Oracle这种现象的重现过程如下:
SQL> create table testnull(id varchar(32));
Table created.
SQL> insert into testnull values('');
1 row created.
SQL> select * from testnull where id is null;
ID
----------------------------------------------------------------
SQL> select count(*) from testnull where id is null;
COUNT(*)
----------
1
SQL>
测试表:testnull(id varchar(32))
数据库:Sybase ASA11.0
行数据(''), (NULL)
数据库选项ansinull为true(也是ASA数据库的默认选项)时,
select * from testnull where id = null
select * from testnull where id != null
结果均为空
select * from testnull where id is null
结果为(NULL)
select * from testnull where id is not null
结果为('')
当ansinull为false时,
select * from testnull where id = null
结果为(NULL)
select * from testnull where id != null
结果为('')
从上述结果来看,NULL值确实是一个有争议的东西,但是,毫无疑问,ansinull对NULL的定义是精确的,即不能对NULL值进行等于或不等判断,无论是等还是不等,其结果都为false.
而统一的is null, is not null的含义则显然是明确的,NULL is null恒为真,非NULL is null恒为假。
再看看在Oracle中的结果:
SQL> select * from testnull where id is null;
ID
--------------------------------
SQL> select * from testnull where id is not null;
no rows selected
SQL> select * from testnull where id=null;
no rows selected
SQL> select * from testnull where id != null;
no rows selected
空字符串''在oracle中被示为NULL值了。比较怪异。
Oracle这种现象的重现过程如下:
SQL> create table testnull(id varchar(32));
Table created.
SQL> insert into testnull values('');
1 row created.
SQL> select * from testnull where id is null;
ID
----------------------------------------------------------------
SQL> select count(*) from testnull where id is null;
COUNT(*)
----------
1
SQL>
相关文章推荐
- 也来说说SQL语句中NULL的真实含义
- SQL语句中NULL的真实含义
- 关于SQL语句中SUM函数返回NULL的解决办法
- LINQ体验(11)——LINQ to SQL语句之Null语义和String/DateTime方法
- [导入]LINQ体验(11)——LINQ to SQL语句之Null语义和String/DateTime方法
- 数据库中存在0,1,2.....或者1,null,2 排序时让0或者null在最后的sql语句
- 必须会的SQL语句(五)NULL数据处理和类型转换
- 必须会的SQL语句(五) NULL数据处理和类型转换
- 在sql语句中的 type in (parem)的含义
- SQL语句中关于null
- 关于SQL语句中SUM函数返回NULL的解决办法
- 黑马程序员—SQL中group by 语句的含义和详细用法
- LINQ体验(11)——LINQ to SQL语句之Null语义和String/DateTime方法
- SQL语句对于NUll的筛选
- 关于SQL语句中SUM函数返回NULL的解决办法
- LINQ体验(11)——LINQ to SQL语句之Null语义和String/DateTime方法
- MyBatis SQL语句汇总SUM值为Null时会抛出转换异常
- SQL语句查询是否为空 =null及null
- SQL语句查询是否为空 =null及null
- LINQ to SQL语句(14)之Null语义和DateTime