MySQL-default设置

Both statements insert a value into the phone column, but the first inserts a NULL value and the second inserts an empty string. The meaning of the first can be regarded as “phone number is not known” and the meaning of the second can be regarded as “the person is known to have no phone, and thus no phone number.”
MySQL :: MySQL 8.0 Reference Manual :: B.5.4.3 Problems with NULL Values

null:未知,未设置值
empty string(''):空,设置但值为空

For MyISAM tables, NULL columns require additional space in the row to record whether their values are NULL. Each NULL column takes one bit extra, rounded up to the nearest byte.
MySQL :: MySQL 5.7 Reference Manual :: C.10.4 Limits on Table Column Count and Row Size

我见网上很多博客写的是在MyISAM中null占一位,empty string('')一位都不占,我感觉有点问题(应该是创建表时设置为null的字段会多一位来存null)但使用information_schema数据库的tables表的DATA_LENGTH字段测试时发现不论插入null还是empty string('')的增量都一样。。

ps:使用select length(''), length(null)查询结果确实是''长度为0

mysql> select length(''), length(null);
+------------+--------------+
| length('') | length(null) |
+------------+--------------+
|          0 |         NULL |
+------------+--------------+

结论:

因为null的处理费劲,且查null的时候据说不走索引,除特殊情况还是用empty string('')方便点(好像和上面没什么关系了)。

猜你喜欢

转载自www.cnblogs.com/jffun-blog/p/9007605.html