MySQL timestamp NOT NULL插入NULL的问题

作者:
淡白
创建时间:
Jul 10, 2020, 2:24:17 PM
数据库 MySQL 799

摘要:遇到如下问题字段gktime不能为null这是表结构:设置字段gktime有默认值解决MySQL5.6版本引入explicit_defaults_for_timestamp来控制对timestampNULL值的处理如果该参数不开启,则对timestampNOTNULL插入NULL值,不报错,无war

遇到如下问题

image.png

字段gktime不能为null
这是表结构:
image.png
设置字段gktime有默认值

解决

MySQL 5.6版本引入

explicit_defaults_for_timestamp

来控制对timestamp NULL值的处理

如果该参数不开启,则对timestamp NOT NULL插入NULL值,不报错,无warning,插入后的值为当前时间

如果在my.cnf中explicit_defaults_for_timestamp=1

那么插入该值的时候会报错提示该列can not be null

设置该值

set global explicit_defaults_for_timestamp=0;

或者修改my.cnf

explicit_defaults_for_timestamp = 0