天天看点

Innodb 下null '' ' '的存储表现的不同

今天顺便测试了一下 他们三者是不同的,简单的说就是

  • null :nullbits 位图上的区别。
  • '':可变字节多一个字节。
  • ' ':可变字节多一个字节且实际数据区域为0X20多一个字节。

如下语句:

mysql> show create table testnull1 \G
*************************** 1. row ***************************
       Table: testnull1
Create Table: CREATE TABLE `testnull1` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  `name1` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> insert into testnull1 values(1,'gaopeng','gaopeng');
Query OK, 1 row affected (0.22 sec)
mysql> insert into testnull1 values(1,'','gaopeng');
Query OK, 1 row affected (0.22 sec)
mysql> insert into testnull1 values(1,null,'gaopeng');
Query OK, 1 row affected (0.22 sec)
mysql> insert into testnull1 values(1,' ','gaopeng');
Query OK, 1 row affected (0.22 sec)

mysql> select * from testnull1;
+------+---------+---------+
| id   | name    | name1   |
+------+---------+---------+
|    1 | gaopeng | gaopeng |
|    1 |         | gaopeng |
|    1 | NULL    | gaopeng |
|    1 |         | gaopeng |
+------+---------+---------+
4 rows in set (0.00 sec)           

主要观察第2,3,4行。

  • 第二行:

07 00:2字节可变字段长度,第二个00代表name 字段的长度,这里''长度是0

00: null位图

0000180025:fixed extrasize

0000012065100000000ec9e9b1000014210110:rowid+trx_id+rollback_ptr

80000001:数据1

67616f70656e67:数据‘gaopeng’

  • 第三行

07:1字节可变长度

02:null位图

0000200026:fixed extrasize

0000012065110000000ec9eeb4000014060110:rowid+trx_id+rollback_ptr

  • 第四行

0701:2字节可变长度,01代表是name字段长度

00:null位图

000028ff78:fixed extrasize

0000012065120000000ec9f0b6000014040110:rowid+trx_id+rollback_ptr

20:数据' '

可以看到他们的区别