MySQL ON UPDATE CURRENT_TIMESTAMP 未更新
我有一个看起来像这样的表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否尝试在更新时对该字段使用 null ?
您还可以尝试将默认值设置为
CURRENT_TIMESTAMP
,而不是0000-00-00 00:00:00
。尽管如此,每当我想要创建和更新时间时,我总是使用以下内容:
我使用
now()
,因为 是CURRENT_TIMESTAMP
的别名,而且它更短。最后,表结构得到CURRENT_TIMESTAMP
,所以不用担心。CREATED
字段的技巧是记住在INSERT
语句的两个字段上都使用 null,而对于UPDATE
语句则不需要:Have you tried to use null for that field when updating?
You could also try setting default value to
CURRENT_TIMESTAMP
, rather than0000-00-00 00:00:00
.Nevertheless, whenever I want to have creation and update time I always use the following:
I use
now()
, because is an alias forCURRENT_TIMESTAMP
and it is shorter. At the end, table structure getsCURRENT_TIMESTAMP
, so don't worry.The trick with
CREATED
field is to remember to use null on both fields forINSERT
statements, forUPDATE
statements it is not required:更新语句可能不会改变任何内容。如果
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 hasBackupstatus
set to'FAIL'
andBackupmsg
set to'Connection timed out'
(maybe, set by some previous backup attempt), then MySQL will skip this row and therefore won't change theLastconnection
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'
您必须记住,如果更新时没有更改值,则不会设置当前时间戳,
您必须在查询
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.
要指定自动属性,请使用
DEFAULT CURRENT_TIMESTAMP
和ON UPDATE CURRENT_TIMESTAMP
子句。子句的顺序并不重要。如果两者都出现在列定义中,则其中一个可以先出现。CURRENT_TIMESTAMP
的任何同义词都与CURRENT_TIMESTAMP
具有相同的含义。它们是CURRENT_TIMESTAMP()
、NOW()
、LOCALTIME
、LOCALTIME()
、LOCALTIMESTAMP
代码>和<代码>LOCALTIMESTAMP()。DEFAULT CURRENT_TIMESTAMP
和ON UPDATE CURRENT_TIMESTAMP
的使用特定于TIMESTAMP
。 DEFAULT 子句还可用于指定常量(非自动)默认值;例如,DEFAULT 0
或DEFAULT '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
andON 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 forCURRENT_TIMESTAMP
have the same meaning asCURRENT_TIMESTAMP
. These areCURRENT_TIMESTAMP()
,NOW()
,LOCALTIME
,LOCALTIME()
,LOCALTIMESTAMP
, andLOCALTIMESTAMP()
.Use of
DEFAULT CURRENT_TIMESTAMP
andON UPDATE CURRENT_TIMESTAMP
is specific toTIMESTAMP
. TheDEFAULT
clause also can be used to specify a constant (nonautomatic) default value; for example,DEFAULT 0
orDEFAULT '2000-01-01 00:00:00'
.DEFAULT 0
do not work if theNO_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 theTRADITIONAL
SQL mode includesNO_ZERO_DATE
.In addition, you can initialize or update any
TIMESTAMP
column to the current date and time by assigning it aNULL
value, unless it has been defined with theNULL
attribute to permitNULL
values.如果您希望记录在更改时自动更新时间戳,则需要完成以下四个简单步骤(可以一步完成,具体取决于您使用命令行还是 GUI 进行管理):
现在,只要记录更新,包含时间戳的字段将始终更新为当前时间戳。
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):
Now the field that contains your timestamp will always be updated to the current timestamp anytime the record is updated.