平时我们在使用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. Leetcode 347. Top K Frequent Elements

    Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...

  2. 【BZOJ-1864】三色二叉树 树形DP

    1864: [Zjoi2006]三色二叉树 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 659  Solved: 469[Submit][Status] ...

  3. Metro-UI系统-1-tile标签

    一 效果图 二 各个效果的详解 1,简单磁贴 <div class="tile" data-role="title"> <!--定义一个磁贴- ...

  4. SSD硬盘的4K对齐

    4K对应4096 硬盘模式: 一.让SSD运行在AHCI模式下: AHCI,全称Advanced Host Controller Interface,即高级主机控制器接口,相比老旧的“IDE“ 虚拟模 ...

  5. Visual Studio 2015出现Cannot find one or more components. Please reinstall the application.的问题解决

    cd C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE devenv.exe /resetuserdata devenv. ...

  6. 本地连接虚拟机上面的redis

    想做一个抓取系统,想到用redis存储临时数据可能会比较好些,就想着装个虚拟机,在虚拟机上面安装redis,通过本地来访问虚拟机上面的redis. 虚拟机和redis安装成功之后,发现本地怎么都连接不 ...

  7. C#用正则表达式对IP进行排序

    static void Main(string[] args) { string IPs = " 192.168.1.1 202.47.4.6 1.2.3.3 "; Console ...

  8. jquery selector 基础

    转自:http://www.cnblogs.com/zwl12549/archive/2008/08/09/1264163.html query的这套选择符是比较帅气的,借用了XPath2.0和CSS ...

  9. NSXMLParser解析本地.xml数据(由于like7xiaoben写的太好了,我从她那里粘贴过来的)

    NSXMLParser解析简要说明 .是sax方法解析 .需要创建NSXMLParser实例 (alloc) 并创建解析器 (initWithData:) 为解析器定义委托 (setDelegate: ...

  10. 【原】javascript数组操作

    继续我的第二遍<javascript高级程序设计第三版>,今天要做的笔记是array 一.数组的操作 1.数组的创建: var colors= new Array(); //创建一个数组 ...