1. 程式人生 > >MySQL-default設置

MySQL-default設置

ID sel ring inf cor AD sam spa have

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(‘‘)方便點(好像和上面沒什麽關系了)。

MySQL-default設置