explicit_defaults_for_timestamp参数的专题报告
| 專題描述 | 解釋explicit_defaults_for_timestamp參數的含義和explicit_defaults_for_timestamp參數 值設定對插入NULL值的影響 | |||
| 問題提出 | OMS系統測試時出現插入數據報錯的問題: 時間字段無法寫入值(下單): b2c1?????????10.202.198.200:3319??oms1/oms1 b2c2?????????10.202.198.201:3319??oms2/oms2 b2c3?????????10.202.198.202:3319??oms3/oms3 b2c4?????????10.202.198.203:3319??oms4/oms4 后續報錯省略。 | |||
| 分析過程 | 1 | 關于定義字段為NOT?NULL,插入NULL值不成功的問題與sql_mode參數的設置沒有關系。 在MySQL中設置參數explicit_defaults_for_timestamp=off的情況下, 對于timestamp?類型列定義為not?null屬性的情況下,插入null值不報錯, 但MySQL會將null值處理為當前時間。 舉例說明: 對于timestamp類型列定義為not?null,沒有default值的情況: root@localhost?:?test?06:17:02>?show?variables?like? 'explicit_defaults_for_timestamp'; +---------------------------------+-------+|?Variable_name???????????????????|?Value?| +---------------------------------+-------+ |?explicit_defaults_for_timestamp?|?OFF????| +---------------------------------+-------+ 1?row?in?set?(0.01?sec) | ||
| test?06:21:33>?show?create?table?t; |?Table?|?Create?Table??????????????????????????????????????????????????????????????????????????????????????????????????? |?t?????|?CREATE?TABLE?`t`?( ??`id`?int(11)?DEFAULT?NULL, ??`t1`?timestamp?NOT?NULL )?ENGINE=InnoDB?DEFAULT?CHARSET=utf8?| 1?row?in?set?(0.00?sec) | ||||
| ?insert?into?t?values(1,null); Query?OK,?1?row?affected?(0.03?sec) root@localhost?:?test?06:22:20>?select?*?from?t; +------+---------------------+ |?id???|?t1??????????????????| +------+---------------------+ |????1?|?2015-06-23?18:22:20?| +------+---------------------+ 1?row?in?set?(0.00?sec) | ||||
| 對于定義timestamp類型列?not?null,有default值的情況: root@localhost?:?test?06:31:16>?show?variables?like? 'explicit_defaults_for_timestamp'; +---------------------------------+-------+|?Variable_name???????????????????|?Value?| +---------------------------------+-------+ |?explicit_defaults_for_timestamp?|?OFF???| +---------------------------------+-------+ 1?row?in?set?(0.00?sec) | ||||
| test?06:31:22>?show?create?table?tt; |?Table?|?Create?Table??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????| |?tt????|?CREATE?TABLE?`tt`?( ??`id`?int(11)?DEFAULT?NULL, ??`t1`?timestamp?NOT?NULL?DEFAULT?'0000-00-00?00:00:00' )?ENGINE=InnoDB?DEFAULT?CHARSET=utf8?| 1?row?in?set?(0.00?sec) | ||||
| test?06:31:29>?insert?into?tt?values(1,null); Query?OK,?1?row?affected?(0.00?sec) root@localhost?:?test?06:31:35>?select?*?from?tt; +------+---------------------+ |?id???|?t1??????????????????| +------+---------------------+ |????1?|?2015-06-23?18:31:35?| +------+---------------------+ 1?row?in?set?(0.01?sec) | ||||
| 解決方案 | explicit_defaults_for_timestamp=off參數僅對于timestamp類型的列有效 ,其它數據類型的列定義為not?null時, 不要插入null值。 | |||
| 問題原因 | explicit_defaults_for_timestamp=off參數僅對于timestamp類型的列有效 ,其它類型的列定義為not?null, 是無法插入null值。 | |||
| 知識點 | explicit_defaults_for_timestamp?=?off值時 ,向定義為timestamp?NOT?NULL的列值中插入NULL值時允許的,且插入值為插入語句當前時間。 當explicit_defaults_for_timestamp?=?ON值時, 不可向timestamp?NOT?NULL列中插入NULL值。 | |||
轉載于:https://blog.51cto.com/yixianwei/1902835
總結
以上是生活随笔為你收集整理的explicit_defaults_for_timestamp参数的专题报告的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度编辑器 UEditor setCon
- 下一篇: 轻量集群管理工具PSSH