插入不工作在mysql 5.7.12-0? [英] insert not working on mysql 5.7.12-0?

查看:193
本文介绍了插入不工作在mysql 5.7.12-0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表结构是

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

我的查询是:

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

这适用于5.7以下的所有MYSQL版本,但不能使用MYSQL版本 5.7 .12-0ubuntu1



获取错误:

  #1364  - 字段age没有默认值

此版本中的新增内容是什么? ?



尝试在低于5.7的mysql版本上,您会看到差异。



谢谢:-)

解决方案

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



age 定义为 varchar(31),而不是null。因此,您的insert语句应该具有该列的值。或者你应该给它一个默认值。

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



更新时间:



考虑到这一点,我认为您已关闭严格模式您的旧版本的mysql


严格模式控制MySQL如何处理无效或缺失值
数据更改语句,如INSERT或UPDATE。值可能是
无效的几个原因。例如,它可能具有错误的数据
类型的列,或者它可能超出范围。当要插入的新行不包含非NULL
列的值时,缺少
值,该值在其定义中没有显式DEFAULT子句。 (对于
NULL列,如果缺少值,则插入NULL。)严格模式
还会影响DDL语句,例如CREATE TABLE。


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


my table structure is

  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 ;

My query is :

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

This is working with all the MYSQL versions below 5.7, but not working with MYSQL version 5.7.12-0ubuntu1

Getting error :

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

What is new in this version ??

Try it on mysql version below 5.7 ,you will see the difference.

Thanks :-)

解决方案

It would be a huge surprise if this worked in any version of mysql at all. Copy paste this into sqlfiddle.com (mysql 5.6 or 5.5) and confirm for yourself.

age is defined as varchar(31) and not null. Thus your insert statement should have a value for that column. Or you should give it a default value. While you are at it, change it to a more appropriate data type.

 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 ;

Updated:

Thinking about this some more I think you have switched off Strict Mode in your older version of mysql

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.

So my original statement is wrong! With string mode off, the default for varchar is probably '' (not sure though never used strict mode off)

这篇关于插入不工作在mysql 5.7.12-0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆