mysql“日期时间非空默认值”1970-01-01“ ”变为 0000-00-00 00:00:00

发布于 2024-09-18 10:20:25 字数 179 浏览 2 评论 0原文

我创建了一个列,

`date_start` datetime NOT NULL DEFAULT '1970-01-01'

但是当我使用 LOAD DATA 命令从 CSV 文件上传数据且 date_start 为空白条目时,保存的值是 0000-00-00 00:00:00 ?

I have a column created as

`date_start` datetime NOT NULL DEFAULT '1970-01-01'

However when I upload data from a CSV file with the LOAD DATA command with a blank entry for date_start the value saved is 0000-00-00 00:00:00 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

枕头说它不想醒 2024-09-25 10:20:25

NULL 和“空白条目”在 MySql 中并不总是被同等对待。对于 datetime 数据类型,空白条目会自动转换为 0000-00-00 00:00:00。您可以将其视为一种类型转换形式,其中空白被类型转换为零。

在您的数据文件中,尝试使用 DEFAULT 关键字替换所有空白日期。

NULL and 'a blank entry' are not always treated the same in MySql. In the case of the datetime data type, blank entries are automatically converted to 0000-00-00 00:00:00. You can think of this as a form of typecasting, where a blank gets typecast to zero.

In your data file, try replacing all blank dates with the DEFAULT keyword.

白色秋天 2024-09-25 10:20:25

默认情况下,MySQL SQL 模式允许零日期。

我相信 LOAD DATA INFILE 命令正在读取旨在作为 DATETIME 的空白位置,并在插入之前自动使用零日期。这可以解释为什么您的默认约束没有被应用 - 插入时该值不为空。

我认为你有两个选择:

  1. 将 CSV 中的所有空白从“, ,”更新为“, NULL,”或“, DEFAULT,”,以便正确解释它们
  2. 更改 SQL 模式SET SQL_MODE='NO_ZERO_DATE'

The MySQL SQL mode by default allows zero dates.

My belief is that the LOAD DATA INFILE command is reading the blank position intended to be a DATETIME, and automatically using a zero date before the insertion. This would explain why your default constraint isn't being applied - the value isn't null when being inserted.

I think you have two options:

  1. Update all the blanks in your CSV from ", ," to ", NULL, " or ", DEFAULT, " so they'll be interpreted correctly
  2. Change the SQL mode: SET SQL_MODE='NO_ZERO_DATE'
夏九 2024-09-25 10:20:25

datetime 表示您的列以 Ymd H:i:s (0000-00-00 00:00:00) 格式存储日期和时间。

date 表示您的列仅以 Ymd (0000-00-00) 格式存储日期。

datetime means your column stores date and time in format Y-m-d H:i:s (0000-00-00 00:00:00).

date means your column stores just date in format Y-m-d (0000-00-00).

一口甜 2024-09-25 10:20:25

如果插入数据时未指定值,则使用默认值,但当您从 CSV 文件插入数据时,它会为字段指定值,因此不使用默认值。

空白条目对于​​日期时间字段来说是无效值,因此将使用值 0000-00-00 00:00:00 来代替,就像任何无法解析为有效值的值一样日期(或部分日期)。

If you don't specify a value when inserting data, the default is used, but when you insert the data from the CSV file it does specify a value for the field, so the default is not used.

A blank entry is an invalid value for a datetime field, so the value 0000-00-00 00:00:00 is used instead, just as with any value that can't be parsed to a valid date (or a partial date).

救星 2024-09-25 10:20:25

MySQL 有一个令人讨厌的(在我看来)行为,在某些情况下,如果给定一个无效值存储在列中,它将存储一个预定义的“错误值”。例如,在日期或日期时间上下文中,它将存储您注意到的“零日期”;对于 ENUM 列,它将存储“”(对应于数字上下文中的 0)作为错误值。

虽然它不太优雅,但您可以尝试进行导入并允许插入无效的零日期,然后发出

UPDATE
    table_name
SET
    date_start = '1970-01-01 00:00:00'
WHERE
    date_start = '0000-00-00 00:00:00'

MySQL has the annoying (in my opinion) behaviour that under some circumstances, if given an invalid value to store in a column, it will store a predefined "error value" instead. For example, in a date or datetime context, it will store the "zero date" you have noticed; for an ENUM column it will store '' (corresponds to 0 in a numeric context) as an error value.

Although it is inelegant, you might try doing the import and allowing the invalid zero dates to be inserted, and then issuing

UPDATE
    table_name
SET
    date_start = '1970-01-01 00:00:00'
WHERE
    date_start = '0000-00-00 00:00:00'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文