gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:57:05 25 4
gpt4 key购买 nike

我的表结构是

  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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com