MySQL ON UPDATE CURRENT_TIMESTAMP 未更新

发布于 2024-10-30 23:44:05 字数 1290 浏览 3 评论 0原文

我有一个看起来像这样的表:

CREATE TABLE IF NOT EXISTS `Hosts` (
`id` int(128) NOT NULL AUTO_INCREMENT,
`IP` varchar(15) NOT NULL DEFAULT '',
`Port` varchar(5) NOT NULL DEFAULT '',
`Password` varchar(32) NOT NULL DEFAULT '',
`Username` varchar(32) NOT NULL DEFAULT '',
`Tid` varchar(32) NOT NULL DEFAULT '',
`EquipType` varchar(64) NOT NULL DEFAULT '',
`Version` varchar(128) DEFAULT NULL,
`Status` varchar(10) NOT NULL DEFAULT '',
`Location` varchar(128) NOT NULL DEFAULT '',
`Lastconnection` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`Lastbackup` date NOT NULL DEFAULT '0000-00-00',
`Backupstatus` varchar(64) NOT NULL DEFAULT '',
`Backupmsg` text,
`Backupfile` varchar(30) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `IP` (`IP`),
KEY `Tid` (`Tid`),
KEY `EquipType` (`EquipType`),
KEY `Status` (`Status`),
KEY `Lastbackup` (`Lastbackup`),
KEY `Backupstatus` (`Backupstatus`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=716 ;

在我看来,这意味着每当更新一行时,字段“Lastconnection”都应该标有当前时间戳。但是,当我运行类似的命令时:

update Hosts set Backupstatus = 'FAIL',  Backupmsg = 'Connection timed out' where Tid = 'SITE001'

Lastconnection 保持“0000-00-00 00:00:00”。要么是我没有看到数据库问题,要么是我完全误解了“ON UPDATE CURRENT_TIMESTAMP”子句。

I've got a table that looks like this:

CREATE TABLE IF NOT EXISTS `Hosts` (
`id` int(128) NOT NULL AUTO_INCREMENT,
`IP` varchar(15) NOT NULL DEFAULT '',
`Port` varchar(5) NOT NULL DEFAULT '',
`Password` varchar(32) NOT NULL DEFAULT '',
`Username` varchar(32) NOT NULL DEFAULT '',
`Tid` varchar(32) NOT NULL DEFAULT '',
`EquipType` varchar(64) NOT NULL DEFAULT '',
`Version` varchar(128) DEFAULT NULL,
`Status` varchar(10) NOT NULL DEFAULT '',
`Location` varchar(128) NOT NULL DEFAULT '',
`Lastconnection` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`Lastbackup` date NOT NULL DEFAULT '0000-00-00',
`Backupstatus` varchar(64) NOT NULL DEFAULT '',
`Backupmsg` text,
`Backupfile` varchar(30) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `IP` (`IP`),
KEY `Tid` (`Tid`),
KEY `EquipType` (`EquipType`),
KEY `Status` (`Status`),
KEY `Lastbackup` (`Lastbackup`),
KEY `Backupstatus` (`Backupstatus`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=716 ;

In my mind, this means that any time a row is updated, the field 'Lastconnection' should be stamped with the current timestamp. However, when I run something like:

update Hosts set Backupstatus = 'FAIL',  Backupmsg = 'Connection timed out' where Tid = 'SITE001'

Lastconnection stays '0000-00-00 00:00:00'. There's either a database issue I'm not seeing, or I'm completely misunderstanding the 'ON UPDATE CURRENT_TIMESTAMP' clause.

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

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

发布评论

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

评论(5

债姬 2024-11-06 23:44:05

您是否尝试在更新时对该字段使用 null ?

您还可以尝试将默认值设置为 CURRENT_TIMESTAMP,而不是 0000-00-00 00:00:00

尽管如此,每当我想要创建和更新时间时,我总是使用以下内容:

...
CREATED timestamp NOT NULL default '0000-00-00 00:00:00',
UPDATED timestamp NOT NULL default now() on update now(),
....

我使用 now(),因为 是 CURRENT_TIMESTAMP 的别名,而且它更短。最后,表结构得到CURRENT_TIMESTAMP,所以不用担心。

CREATED 字段的技巧是记住在 INSERT 语句的两个字段上都使用 null,而对于 UPDATE 语句则不需要:

INSERT INTO mytable (field1, field2, created, updated)
VALUES ('foo', 'bar', null, null);

Have you tried to use null for that field when updating?

You could also try setting default value to CURRENT_TIMESTAMP, rather than 0000-00-00 00:00:00.

Nevertheless, whenever I want to have creation and update time I always use the following:

...
CREATED timestamp NOT NULL default '0000-00-00 00:00:00',
UPDATED timestamp NOT NULL default now() on update now(),
....

I use now(), because is an alias for CURRENT_TIMESTAMP and it is shorter. At the end, table structure gets CURRENT_TIMESTAMP, so don't worry.

The trick with CREATED field is to remember to use null on both fields for INSERT statements, for UPDATE statements it is not required:

INSERT INTO mytable (field1, field2, created, updated)
VALUES ('foo', 'bar', null, null);
掀纱窥君容 2024-11-06 23:44:05

更新语句可能不会改变任何内容。如果 Tid = 'SITE001' 的行已将 Backupstatus 设置为 'FAIL' 并将 Backupmsg 设置为 < code>'连接超时'(可能是由之前的备份尝试设置的),那么 MySQL 将跳过此行,因此不会更改 Lastconnection 时间戳。

另外,我认为 ON UPDATE CURRENT_TIMESTAMP 更像是跟踪数据更改的管理功能。作为一名程序员,我会显式添加时间戳更新:

更新主机
设置 Backupstatus = 'FAIL', Backupmsg = '连接超时', Lastconnection = NOW()
其中 Tid = 'SITE001'

It might be the case that the update statement doesn't change anything. If the row with Tid = 'SITE001' already has Backupstatus set to 'FAIL' and Backupmsg set to 'Connection timed out' (maybe, set by some previous backup attempt), then MySQL will skip this row and therefore won't change the Lastconnection timestamp.

Also, I see ON UPDATE CURRENT_TIMESTAMP more like an administrative feature to keep track of data changes. As a programmer, I would add the timestamp update explicitly:

update Hosts
set Backupstatus = 'FAIL', Backupmsg = 'Connection timed out', Lastconnection = NOW()
where Tid = 'SITE001'
.

睫毛上残留的泪 2024-11-06 23:44:05

您必须记住,如果更新时没有更改值,则不会设置当前时间戳,

您必须在查询NOW()中设置值以设置当前时间戳!!!

update Hosts set Backupstatus = 'FAIL',Lastconnection = NOW() , Backupmsg = '连接超时' where Tid = 'SITE001'

请记住,必须更改该值才能更改当前时间戳。

You must remember that if no value was changed on update it won't set the current time stamp,

You have to set the value in query NOW() to set for the current time stamp !!!

update Hosts set Backupstatus = 'FAIL',Lastconnection = NOW() , Backupmsg = 'Connection timed out' where Tid = 'SITE001'

Remember the value must change in order for the current time stamp to change.

鯉魚旗 2024-11-06 23:44:05

要指定自动属性,请使用 DEFAULT CURRENT_TIMESTAMPON UPDATE CURRENT_TIMESTAMP 子句。子句的顺序并不重要。如果两者都出现在列定义中,则其中一个可以先出现。 CURRENT_TIMESTAMP 的任何同义词都与 CURRENT_TIMESTAMP 具有相同的含义。它们是CURRENT_TIMESTAMP()NOW()LOCALTIMELOCALTIME()LOCALTIMESTAMP代码>和<代码>LOCALTIMESTAMP()。

DEFAULT CURRENT_TIMESTAMPON UPDATE CURRENT_TIMESTAMP 的使用特定于 TIMESTAMP。 DEFAULT 子句还可用于指定常量(非自动)默认值;例如,DEFAULT 0DEFAULT '2000-01-01 00:00:00'

如果启用了 NO_ZERO_DATE SQL 模式,则 DEFAULT 0 不起作用,因为该模式会导致“零”日期值(例如指定为 0 '0000-00-00 00 :00:00') 被拒绝。请注意,TRADITIONAL SQL 模式包括 NO_ZERO_DATE

此外,您可以通过为任何 TIMESTAMP 列分配 NULL 值来将其初始化或更新为当前日期和时间,除非已使用 NULL< 定义了该列/code> 属性允许 NULL 值。

To specify automatic properties, use the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses. The order of the clauses does not matter. If both are present in a column definition, either can occur first. Any of the synonyms for CURRENT_TIMESTAMP have the same meaning as CURRENT_TIMESTAMP. These are CURRENT_TIMESTAMP(), NOW(), LOCALTIME, LOCALTIME(), LOCALTIMESTAMP, and LOCALTIMESTAMP().

Use of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is specific to TIMESTAMP. The DEFAULT clause also can be used to specify a constant (nonautomatic) default value; for example, DEFAULT 0 or DEFAULT '2000-01-01 00:00:00'.

DEFAULT 0 do not work if the NO_ZERO_DATE SQL mode is enabled because that mode causes “zero” date values (specified, for example, as 0 '0000-00-00 00:00:00') to be rejected. Be aware that the TRADITIONAL SQL mode includes NO_ZERO_DATE.

In addition, you can initialize or update any TIMESTAMP column to the current date and time by assigning it a NULL value, unless it has been defined with the NULL attribute to permit NULL values.

屌丝范 2024-11-06 23:44:05

如果您希望记录在更改时自动更新时间戳,则需要完成以下四个简单步骤(可以一步完成,具体取决于您使用命令行还是 GUI 进行管理):

  1. 创建字段保存自动更新的时间戳(我通常称我的为“修改的”)。
  2. 将字段类型指定为“TIMESTAMP”
  3. 将字段默认值指定为“CURRENT_TIMESTAMP”
  4. 将额外字段指定为“ON UPDATE CURRENT_TIMESTAMP”

现在,只要记录更新,包含时间戳的字段将始终更新为当前时间戳。

If you want the record to automatically update the timestamp whenever the record is changed, here's the four simple steps you need to accomplish (could be all in one step, depending on if you're using command line or GUI to administer):

  1. Create field to hold auto-updated timestamp (I typically call mine 'modified').
  2. Specify field Type as 'TIMESTAMP'
  3. Specify field Default as 'CURRENT_TIMESTAMP'
  4. Specify field Extra as 'ON UPDATE CURRENT_TIMESTAMP'

Now the field that contains your timestamp will always be updated to the current timestamp anytime the record is updated.

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