Usually when we use MySQL, we cannot understand the difference between NULL values and null values in MySQL. Note that the NULL value is unknown and takes up space without indexing. DBA suggests that it is best to set the field to NOT NULL when building a table to avoid such inefficiencies.
Question 1: First of all, we need to understand the concept of "null value" and "NULL":
1: Null value ('') does not occupy space
2: NULL in MySQL actually occupies space. The official documentation states:
"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."
Length verification: pay attention to the '' of null values There are no spaces in between.
mysql> select length(''),length(null),length(' ');
+------------+--------------+- -------------+
| length('') | length(null) | length(' '
Question 2:
When judging that the field is not empty, whether the query statement uses select * from tablename where columnname <> '' or
select * from tablename where column is not null, what is the difference between the two query statements.
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)
Notes:
1: When count() counts the number of records in a column, if the NULL value is used, the system will automatically ignore it, but the NULL value will be counted into it.
2: Use IS NULL or is not null to judge NULL, you can use the ifnull() function in the SQL statement function to process it, and use ='' or <>'' to process the null character
3: For MySQL special precautions, For the timestamp data type, if a NULL value is inserted into a column of this data type, the value that appears is the current system time. Insert a null value, and '0000-00-00 00:00:00' will appear.
4: Whether to use is null or ='' for the judgment of null value should be distinguished according to the actual business.
From: http://blog.sina.com.cn/s/blog_3f2a82610102v4dn.html
Difference between NULL and empty value in MySQL
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326441345&siteId=291194637
Recommended
Ranking