插入零而不是null的空字符串 [英] Empty string inserting a zero, not a null

查看:91
本文介绍了插入零而不是null的空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的插入语句如下:

INSERT INTO foo (bar) VALUES ('');

bar字段的创建方式如下:

The bar field was created like so:

bar INT(11)
    COLLATION: (NULL)
    NULL: YES
    DEFAULT: (NULL)

MySQL版本:5.1.

MySQL version: 5.1.

一个空字符串不应该插入NULL吗?我不确定为什么表格中会存储零(0).

Shouldn’t an empty string insert a NULL? I’m not sure why I’m seeing a zero (0) being stored in the table.

推荐答案

MySQL默认情况下会尝试将列的无效值强制转换为正确的类型.在这里,空字符串''的类型为string,它既不是整数,也不是NULL.我建议采取以下步骤:

MySQL by default attempts to coerce invalid values for a column to the correct type. Here, the empty string '' is of type string, which is neither an integer nor NULL. I suggest taking the following steps:

  1. 将查询更改为以下内容:INSERT INTO foo (bar) VALUES (NULL);
  2. 在MySQL中启用严格模式.这样可以防止发生许多意外的类型和值转换.尝试执行MySQL所不希望的操作时,您会看到更多错误消息,这有助于您更快地发现问题.

这篇关于插入零而不是null的空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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