从MySQL 5.7升级到MySQL 8后,无法将空值插入非空日期字段
我从MySQL 5.7.26升级到了8.0.20 其中一个表的not null列具有TIMESTAMP数据类型,默认值为当前\u TIMESTAMP从MySQL 5.7升级到MySQL 8后,无法将空值插入非空日期字段,mysql,sql-insert,mysql-5.7,mysql-8.0,sql-null,Mysql,Sql Insert,Mysql 5.7,Mysql 8.0,Sql Null,我从MySQL 5.7.26升级到了8.0.20 其中一个表的not null列具有TIMESTAMP数据类型,默认值为当前\u TIMESTAMP mysql>insert into test_field ( object_type, dataType, fieldSize, hiddenFlag , fieldLabel, fieldName, modified_date) values ('LEAD', 13, 44, 1, 'Date Last Updated_testing1',
mysql>insert into test_field ( object_type, dataType, fieldSize, hiddenFlag , fieldLabel, fieldName, modified_date) values ('LEAD', 13, 44, 1, 'Date Last Updated_testing1', 'test_Insert_test1', NULL);
在5.7.26,
如果像下面这样的insert语句被运行,它将被成功执行,NOTNULL列将被默认值(即当前_时间戳)填充
mysql>insert into test_field ( object_type, dataType, fieldSize, hiddenFlag , fieldLabel, fieldName, modified_date) values ('LEAD', 13, 44, 1, 'Date Last Updated_testing1', 'test_Insert_test1', NULL);
查询正常,1行受影响,1条警告(0.00秒)
升级到8.0.20后,出现错误:
mysql>insert into test_field ( object_type, dataType, fieldSize, hiddenFlag , fieldLabel, fieldName, modified_date) values ('LEAD', 13, 44, 1, 'Date Last Updated_testing1', 'test_Insert_test1', NULL);
错误1048(23000):“修改日期”列不能为空
我不认为MySQL在5.7版和8.0版之间,将
null
插入设置了default
值的非null
列的行为发生了变化。但是,这种情况下发生的情况取决于是否启用了严格模式:
- 如果启用了严格SQL模式,事务表将发生错误,语句将回滚。对于非事务表,会发生错误,但如果多行语句的第二行或后续行发生错误,则会插入前面的行
- 如果未启用严格模式,MySQL将列设置为列数据类型的隐式默认值
DEFAUT
而不是NULL
:
insert into test_field (object_type, dataType, fieldSize, hiddenFlag , fieldLabel, fieldName, modified_date)
values ('LEAD', 13, 44, 1, 'Date Last Updated_testing1', 'test_Insert_test1', DEFAULT);
我不认为MySQL在5.7版和8.0版之间,将
null
插入设置了default
值的非null
列的行为发生了变化。但是,这种情况下发生的情况取决于是否启用了严格模式:
- 如果启用了严格SQL模式,事务表将发生错误,语句将回滚。对于非事务表,会发生错误,但如果多行语句的第二行或后续行发生错误,则会插入前面的行
- 如果未启用严格模式,MySQL将列设置为列数据类型的隐式默认值
DEFAUT
而不是NULL
:
insert into test_field (object_type, dataType, fieldSize, hiddenFlag , fieldLabel, fieldName, modified_date)
values ('LEAD', 13, 44, 1, 'Date Last Updated_testing1', 'test_Insert_test1', DEFAULT);
你能给我们看一个create表吗?要复制它,还可以看看MySQL 8+中的情况。你需要指定它是
默认值
还是非
。当您编写null
这是一个通用属性时,它会尝试插入null
,但由于它是notnull
,所以使用default
,它会起作用。创建表test_field
(id
int not null AUTO_INCREMENT,fieldName
varchar(100)不为空,fieldLabel
varchar(100)默认为空,object\u type
varchar(50)不为空,creation\u date
timestamp不为空默认为当前\u时间戳,hiddenfag
tinyint(1)不为空默认值“0”,主键(id
),唯一键ukey\u object\u fieldName
(object\u type
,fieldName
),键fkey\u cf\u datatype
(datatype
)引擎=InnoDB默认字符集=utf8;你能给我们看一个create表吗?要复制它,还可以看看MySQL 8+中的情况。你需要指定它是默认值
还是非
。当您编写null
这是一个通用属性时,它会尝试插入null
,但由于它是notnull
,所以使用default
,它会起作用。创建表test_field
(id
int not null AUTO_INCREMENT,fieldName
varchar(100)不为空,fieldLabel
varchar(100)默认为空,object\u type
varchar(50)不为空,creation\u date
timestamp不为空默认为当前\u时间戳,hiddenfag
tinyint(1)不为空默认值“0”,主键(id
),唯一键ukey\u object\u fieldName
(object\u type
,fieldName
),键fkey\u cf\u datatype
(datatype
)引擎=InnoDB默认字符集=utf8;两个版本在全局上都有innodb_strict_模式这两个版本在全局上都有innodb_strict_模式