mysql - 插入不适用于 mysql 5.7.12-0?

标签 mysql sql database mysql-error-1064

我的表结构是

  CREATE TABLE IF NOT EXISTS `emp` (
 `id` int(3) NOT NULL AUTO_INCREMENT,
 `name` varchar(11) DEFAULT NULL,
 `age` varchar(31) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

我的查询是:

 INSERT INTO `emp` (`id`, `name`) VALUES ('1', 'prashant');

这适用于 5.7 以下的所有 MYSQL 版本,但不适用于 MYSQL 版本 5.7.12-0ubuntu1

获取错误:

#1364 - Field 'age' doesn't have a default value

这个版本有什么新东西??

在5.7以下的mysql版本上试试,你会看到区别。

谢谢 :-)

最佳答案

如果这在任何版本的 mysql 中都有效,那将是一个巨大的惊喜。将其复制粘贴到 sqlfiddle.com(mysql 5.6 或 5.5)并自行确认。

age 定义为 varchar(31) 而不是 null。因此,您的插入语句应该具有该列的值。或者你应该给它一个默认值。当您使用它时,将其更改为更合适的数据类型。

 CREATE TABLE IF NOT EXISTS `emp` (
 `id` int(3) NOT NULL AUTO_INCREMENT,
 `name` varchar(11) DEFAULT NULL,
 `age` int(3) NOT NULL default 0,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

更新:

再考虑一下我认为你已经关闭了旧版本的 mysql 中的 Strict Mode

Strict mode controls how MySQL handles invalid or missing values in data-change statements such as INSERT or UPDATE. A value can be invalid for several reasons. For example, it might have the wrong data type for the column, or it might be out of range. A value is missing when a new row to be inserted does not contain a value for a non-NULL column that has no explicit DEFAULT clause in its definition. (For a NULL column, NULL is inserted if the value is missing.) Strict mode also affects DDL statements such as CREATE TABLE.

所以我原来的说法是错误的!关闭字符串模式后,varchar 的默认值可能是 ''(不确定,但从未关闭过严格模式)

关于mysql - 插入不适用于 mysql 5.7.12-0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38374536/

相关文章:

django - 使用 django 1.9 执行 syncdb 时出现 “settings.DATABASES is improperly configured” 错误

mysql - 如何重置忘记的 MySQL root 密码?

mysql - SQL中玩家得分表的结构

mysql - 如何从选择中进行选择?

sql - (PL/SQL) 创建新数据库时崩溃

sql - 在SQLServer 2012 TSQL中,使用XML RAW,XML AUTO和XML PATH有什么区别

php - 按另一个表的计数排序?

MySQL C API : Need example for initiating DB on *Embedded* mode

java - hibernate/JPA : How to find sub entities using InheritanceType. 已加入

mysql - sqlite 插入同时删除完整的重复项