今天遇到一個問題,本身在本地數據執行插入sql沒啥毛病,但上了測試環境就報錯!找個許久發現和mysql的配置有關。查看本地數據庫
select @@sql_mode
爲空,測試環境執行發現:
+---------------------+
| @@sql_mode |
+---------------------+
| STRICT_TRANS_TABLES |
+---------------------+
這種模式下相當於給sql加入了數據,語法的校驗,如果有設置not null 但沒有default值的字段就會報
doesn't have a default value的錯誤。
set sql_mode =""; 執行完就好了;
所以再創建表的時候,把一些非必須的字段最好都加上not null 的同時加上 default ;
爲什麼要把字段設置成not null 呢?
1、空值是不佔用空間的
2、mysql中的NULL其實是佔用空間的,下面是來自於MYSQL官方的解釋
“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.”
空和null查詢的條件也是不一樣的,所有最好設置not null!