mysql 插入not null 沒有default報錯(doesn't have a default value)

來源:互聯網
上載者:User

標籤:mysql insert doesn't have a default value

今天遇到一個問題,本身在本機資料執行插入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!

本文出自 “7804265” 部落格,請務必保留此出處http://7814265.blog.51cto.com/7804265/1789338

mysql 插入not null 沒有default報錯(doesn't have a default value)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.