oracle 中的null与''

1.先看看Null与''在oracle中的表现

C:Userszen>sqlplus hr/hr

SQL*Plus: Release 11.2.0.1.0 Production on Fri Mar 31 10:30:32 2017

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> create table test_null(id_ number,name_ varchar2(10));

Table created.

SQL> insert into test_null values(1,'oracle');

1 row created.

SQL> insert into test_null values(2,'');

1 row created.

SQL> insert into test_null values(3,null);

1 row created.

SQL> select * from test_null;

       ID_ NAME_
---------- ----------
         1 oracle
         2
         3

SQL> select nvl(name_,'It is null') nvl_null,nvl('','It is empty string') emptystr from test_null;

NVL_NULL   EMPTYSTR
---------- ------------------
oracle     It is empty string
It is null It is empty string
It is null It is empty string

SQL> select * from test_null where name_ is null;

       ID_ NAME_
---------- ----------
         2
         3

SQL> select * from test_null where name_='';

no rows selected

SQL> select * from test_null where cast(name_ as varchar2(10))='';

no rows selected

SQL> select * from test_null where cast(name_ as varchar2(10))=cast('' as varchar2(10));

no rows selected

SQL> select * from test_null where name_<>'';

no rows selected

SQL> select * from test_null where nvl(name_,'')='';


no rows selected


SQL> select * from test_null where nvl(name_,'A')='A';


ID_ NAME_
---------- ----------
2
3


SQL>

2.关于以上现象的解释

oracle 将'' 当成了null 处理。每个null都是独一无二的,对null的操作只能是 is null OR is not null,对于null的=<>,>,<的逻辑判断都会得到否。

3.看看null与''在Mysql中的表现

C:Userszen>mysql -uzen -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> use product_test;
Database changed
mysql> drop table test_null;
Query OK, 0 rows affected (0.37 sec)

mysql> create table test_null(id_ int,name_ varchar(127));
Query OK, 0 rows affected (0.59 sec)

mysql> insert into test_null values(1,'oracle'),(2,''),(3,null);
Query OK, 3 rows affected (0.08 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from test_null;
+------+--------+
| id_  | name_  |
+------+--------+
|    1 | oracle |
|    2 |        |
|    3 | NULL   |
+------+--------+
3 rows in set (0.00 sec)

mysql> select * from test_null where name_ is null;
+------+-------+
| id_  | name_ |
+------+-------+
|    3 | NULL  |
+------+-------+
1 row in set (0.06 sec)

mysql> select * from test_null where name_='';
+------+-------+
| id_  | name_ |
+------+-------+
|    2 |       |
+------+-------+
1 row in set (0.00 sec)

mysql> select * from test_null where name_<>'';
+------+--------+
| id_  | name_  |
+------+--------+
|    1 | oracle |
+------+--------+
1 row in set (0.00 sec)

mysql> select * from test_null where name_ is not null;
+------+--------+
| id_  | name_  |
+------+--------+
|    1 | oracle |
|    2 |        |
+------+--------+
2 rows in set (0.00 sec)

mysql>

4.在mysql中null 就是null,''就是空字符,没有将二者混淆起来。

原文地址:https://www.cnblogs.com/Alex-Zeng/p/6650529.html