平时我们在使用MySQL的时候,对于MySQL中的NULL值和空值区别不能很好的理解。注意到NULL值是未知的,且占用空间,不走索引,DBA建议建表的时候最好设置字段是NOT NULL 来避免这种低效率的事情的发生。
   问题 1: 首先,我们需要搞清楚 "空值" 和"NULL"的概念:
    1:空值('')是不占用空间的
    2: MySQL中的NULL其实是占用空间的。官方文档说明:
“NULL columns require additional space in the row to record whether their values are NULL. For MyISAM tables, each NULL column takes one bit extra, rounded up to the nearest byte.” 
长度验证:注意空值的''之间是没有空格的。
mysql> select length(''),length(null),length('  ');
+------------+--------------+--------------+
| length('') | length(null) | length('  ') |
+------------+--------------+--------------+
|          0 |         NULL |            2 |
+------------+--------------+--------------+
 
   问题2:
判断字段不为空的时候,查询语句到底是用 select * from  tablename  where columnname <> '' 还是用
select * from tablename where column is not null,2个查询语句有啥不同。

eg:

mysql> show create table testaa;
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                                                                                         |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| testaa | CREATE TABLE `testaa` (
  `a` int(11) NOT NULL,
  `b` varchar(20) DEFAULT NULL,
  `c` varchar(20) NOT NULL,
  PRIMARY KEY (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
 
插入测试数据:
mysql> insert testaa  values (1,'aa','');
Query OK, 1 row affected (0.00 sec)
mysql> insert testaa  values (2,'','');
Query OK, 1 row affected (0.00 sec)
mysql> insert testaa  values (3,null,'');
Query OK, 1 row affected (0.00 sec)
mysql> insert testaa  values (4,NULL,'');
Query OK, 1 row affected (0.00 sec)
mysql> insert testaa  values (5,'aafa','fa');
Query OK, 1 row affected (0.00 sec)
 
mysql> insert testaa values (6,'',NULL);
ERROR 1048 (23000): Column 'c' cannot be null
 
mysql> select * from testaa;
+---+------+----+
| a | b    | c  |
+---+------+----+
| 1 | aa   |    |
| 2 |      |    |
| 3 | NULL |    |
| 4 | NULL |    |
| 5 | aafa | fa |
+---+------+----+
 
查询验证过程:
 
mysql> select * from testaa where c is not null;
+---+------+----+
| a | b    | c  |
+---+------+----+
| 1 | aa   |    |
| 2 |      |    |
| 3 | NULL |    |
| 4 | NULL |    |
| 5 | aafa | fa |
+---+------+----+
5 rows in set (0.00 sec)
 
mysql> select * from testaa where c <> '';
+---+------+----+
| a | b    | c  |
+---+------+----+
| 5 | aafa | fa |
+---+------+----+
1 row in set (0.00 sec)
mysql> select * from testaa  where c = '';
+---+------+---+
| a | b    | c |
+---+------+---+
| 1 | aa   |   |
| 2 |      |   |
| 3 | NULL |   |
| 4 | NULL |   |
+---+------+---+
4 rows in set (0.00 sec) 
 
mysql> select * from testaa where  c is null;
Empty set (0.00 sec)
 
 
mysql> select * from testaa where b is not null;
+---+------+----+
| a | b    | c  |
+---+------+----+
| 1 | aa   |    |
| 2 |      |    |
| 5 | aafa | fa |
+---+------+----+
3 rows in set (0.00 sec)
 
mysql> select * from testaa where b <> '';
+---+------+----+
| a | b    | c  |
+---+------+----+
| 1 | aa   |    |
| 5 | aafa | fa |
+---+------+----+
2 rows in set (0.00 sec)
 
mysql> select * from testaa where b ='';
+---+------+---+
| a | b    | c |
+---+------+---+
| 2 |      |   |
+---+------+---+
1 row in set (0.00 sec)
mysql> select * from testaa where  b is null;
+---+------+---+
| a | b    | c |
+---+------+---+
| 3 | NULL |   |
| 4 | NULL |   |
+---+------+---+
 
mysql> select length(b),length(c) from testaa;
+-----------+-----------+
| length(b) | length(c) |
+-----------+-----------+
| 2 | 0 |
| 0 | 0 |
| NULL | 0 |
| NULL | 0 |
| 4 | 2 |
+-----------+-----------+
5 rows in set (0.00 sec)
 
 
mysql> select count(b),count(c) from testaa;
+----------+----------+
| count(b) | count(c) |
+----------+----------+
| 3 | 5 |
+----------+----------+
1 row in set (0.00 sec)
 
mysql> create table testbb ( a int primary key , b timestamp);
Query OK, 0 rows affected (0.07 sec)
mysql> show create table testbb;
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| testbb | CREATE TABLE `testbb` (
`a` int(11) NOT NULL,
`b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 
mysql> insert into testbb vales (1,null) ;
mysql> insert into testbb values (2,'');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> show warnings;
+---------+------+----------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------+
| Warning | 1265 | Data truncated for column 'b' at row 1 |
+---------+------+----------------------------------------+
mysql> select * from testbb;
+---+---------------------+
| a | b |
+---+---------------------+
| 1 | 2014-08-15 14:32:10 |
| 2 | 0000-00-00 00:00:00 |
+---+---------------------+
2 rows in set (0.00 sec)
 
 
注意事项:
1:在进行count()统计某列的记录数的时候,如果采用的NULL值,会别系统自动忽略掉,但是空值是会进行统计到其中的。
2: 判断NULL 用IS NULL 或者 is not null,SQL 语句函数中可以使用ifnull()函数来进行处理,判断空字符用 =''或者 <>''来进行处理
3: 对于MySQL特殊的注意事项,对于timestamp数据类型,如果往这个数据类型插入的列插入NULL值,则出现的值是当前系统时间。插入空值,则会出现 '0000-00-00 00:00:00'
4:对于空值的判断到底是使用is null 还是 =''要根据实际业务来进行区分。

MySQL 中NULL和空值的区别的更多相关文章

  1. MySQL 中NULL和空值的区别 (转载 http://blog.sina.com.cn/s/blog_3f2a82610102v4dn.html)

    平时我们在使用MySQL的时候,对于MySQL中的NULL值和空值区别不能很好的理解.注意到NULL值是未知的,且占用空间,不走索引,DBA建议建表的时候最好设置字段是NOT NULL 来避免这种低效 ...

  2. MySQL 中NULL和空值的区别,索引列可以有空值或者null吗?

    空值跟null的区别.mysql官方: “NULL columns require additional space in the row to record whether their values ...

  3. 【面试】MySQL 中NULL和空值的区别?

    做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 01 小木的故事 作为后台开发,在日常工作中如果要接触Mysql数据库,那么不可避免会遇到Mysql中的NULL和空值.那 ...

  4. mysql中null与“空值”的坑

    https://blog.csdn.net/u014743697/article/details/54136092

  5. 用count(*)还是count(列名) || Mysql中的count()与sum()区别

    Mysql中的count()与sum()区别   首先创建个表说明问题 CREATE TABLE `result` (   `name` varchar(20) default NULL,   `su ...

  6. MySQL中interactive_timeout和wait_timeout的区别【转】

    在用mysql客户端对数据库进行操作时,打开终端窗口,如果一段时间没有操作,再次操作时,常常会报如下错误: ERROR 2013 (HY000): Lost connection to MySQL s ...

  7. MySQL中 utf8与utf8mb4的区别

    MySQL中 utf8与utf8mb4的区别 一.简介 ​ MySQL在5.5.3之后增加了这个utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode.好在 ...

  8. (转)MySQL中In与Exists的区别

    背景:总结mysql相关的知识点. 如果A表有n条记录,那么exists查询就是将这n条记录逐条取出,然后判断n遍exists条件. select * from user where exists s ...

  9. Mysql中函数和存储过程的区别

    Mysql中函数和存储过程的区别 存储过程: 1.       可以写sql语句 2.       inout,out构造返回值 3.       调用:call:存储过程名称 4.       可以 ...

随机推荐

  1. BZOJ 1103: [POI2007]大都市meg

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2189  Solved: 1160[Submit][Sta ...

  2. 【BZOJ-1069】最大土地面积 计算几何 + 凸包 + 旋转卡壳

    1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2707  Solved: 1053[Submit][Sta ...

  3. 【BZOJ-2440】完全平方数 容斥原理 + 线性筛莫比乌斯反演函数 + 二分判定

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2371  Solved: 1143[Submit][Sta ...

  4. springMVC-数据的格式化

    1.配置annotation-driven <mvc:annotation-driven ></mvc:annotation-driven> 2.在实体类上加上@NumberF ...

  5. 【codevs1993】 草地排水

    http://codevs.cn/problem/1993/ (题目链接) 题意 求有向图最大流. Solution Dinic. 代码 // codevs1993 #include<algor ...

  6. 【poj1019】 Number Sequence

    http://poj.org/problem?id=1019 (题目链接) 题意 给出一个数:1 12 123 1234 12345 123456 1234567 12345678 123456789 ...

  7. exists的用法

    今天突然看到之前自己写的一个代码 久久没想通为毛.. 看来笔记还是需要 exists可以代替子查询in  比in 更高效   默认是exists 查询中包含有数据则条件成立..否则没数据 select ...

  8. vijos2001 xor-sigma

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  9. 利用iconv进行文件编码批量原地转换

    将当前目录及其所有子目录中的以 java 为后缀的文件,从 GB18030 转换为 UTF-8: find .  -name "*.java" -exec sh -c " ...

  10. 计算机组成原理 及CPU,硬盘,内存三者的关系

    前面提到了,电脑之父——冯·诺伊曼提出了计算机的五大部件:输入设备.输出设备.存储器.运算器和控制器. 我们看一下现在我们电脑的: 键盘鼠标.显示器.机箱.音响等等. 这里显示器为比较老的CRT显示器 ...