mysql - MariaDB 时间戳不是更新日期

标签 mysql mariadb

我刚刚在表中添加了一个时间戳字段

我需要该字段来保存上次更新,而不向每个查询添加指令

 `lastupd` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

问题是所有字段都有 0000-00-00 00:00:00 并且在查询更新时 timestamp 未更新

我使用 5.5.60-MariaDB - MariaDB 服务器并使用 MyISAM

最佳答案

(如果我没记错的话)在旧版本中,这样的 DEFAULT 只能应用于表中的第一个 TIMESTAMP。请提供SHOW CREATE TABLE

请考虑切换到 InnoDB。

来自 5.6.6 变更日志:

In MySQL, the TIMESTAMP data type differs in nonstandard ways from other data types:

TIMESTAMP columns not explicitly declared with the NULL attribute are assigned the NOT NULL attribute. (Columns of other data types, if not explicitly declared as NOT NULL, permit NULL values.) Setting such a column to NULL sets it to the current timestamp.

The first TIMESTAMP column in a table, if not declared with the NULL attribute or an explicit DEFAULT or ON UPDATE clause, is automatically assigned the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP attributes.

TIMESTAMP columns following the first one, if not declared with the NULL attribute or an explicit DEFAULT clause, are automatically assigned DEFAULT '0000-00-00 00:00:00' (the “zero” timestamp). For inserted rows that specify no explicit value for such a column, the column is assigned '0000-00-00 00:00:00' and no warning occurs.

Those nonstandard behaviors remain the default for TIMESTAMP but now are deprecated and this warning appears at startup:

[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

As indicated by the warning, to turn off the nonstandard behaviors, enable the new explicit_defaults_for_timestamp system variable at server startup. With this variable enabled, the server handles TIMESTAMP as follows instead:

TIMESTAMP columns not explicitly declared as NOT NULL permit NULL values. Setting such a column to NULL sets it to NULL, not the current timestamp.

No TIMESTAMP column is assigned the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes automatically. Those attributes must be explicitly specified.

TIMESTAMP columns declared as NOT NULL and without an explicit DEFAULT clause are treated as having no default value. For inserted rows that specify no explicit value for such a column, the result depends on the SQL mode. If strict SQL mode is enabled, an error occurs. If strict SQL mode is not enabled, the column is assigned the implicit default of '0000-00-00 00:00:00' and a warning occurs. This is similar to how MySQL treats other temporal types such as DATETIME.

关于mysql - MariaDB 时间戳不是更新日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54234413/

相关文章:

c# - 验证无效字符串值 Mysql : '\xD9\xBE\xD8\xA7\xDA\xA9

mysql - 如何在mysql的drop table语句中使用变量?

mysql - 计算 MySQL 中列的简单中位数

创建新用户时出现Mysql问题

Mysql如何根据另一个不同列查找不同列的计数?

php - 合并不同列表的搜索结果并按日期排序

javascript - 单击背景颜色更改并保存在数据库中

bash 脚本没有正确给出 MariaDB 服务状态

java - Prepared Statements 额外引号

PHP session 数组值