时间戳记(自动)何时更新? [英] When is a timestamp (auto) updated?

查看:111
本文介绍了时间戳记(自动)何时更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在类型为TIMESTAMP的表中有一列并且具有默认值:CURRENT_TIMESTAMP,如果我更新该值,此列是否会更新为当前时间戳 同一行中的任何其他列?
似乎没有,但是我不确定这是否应该发生.
我不明白这是什么意思(来自MySQL文档):

If I have a column in a table of type TIMESTAMP and has as default: CURRENT_TIMESTAMP does this column get updated to the current timestamp if I update the value of any other column in the the same row?
It seems that it does not but I am not sure if this is what should happen.
I can not understand what this means (from MySQL documentation):

如果该列是自动更新的,则会自动更新为 当行中任何其他列的值为 从当前值更改.如果所有,则该列保持不变 其他列设置为其当前值.防止立柱 从更新其他列更改时,显式地将其设置为 当前值.即使其他列未更新,也要更新该列 更改,将其显式设置为应具有的值] 2

If the column is auto-updated, it is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. The column remains unchanged if all other columns are set to their current values. To prevent the column from updating when other columns change, explicitly set it to its current value. To update the column even when other columns do not change, explicitly set it to the value it should have]2

推荐答案

给出命令SHOW CREATE TABLE whatever

然后查看表定义.

它可能有这样的一行

logtime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

在其中. DEFAULT CURRENT_TIMESTAMP表示没有明确时间戳设置的任何INSERT都使用当前时间.同样,ON UPDATE CURRENT_TIMESTAMP表示没有显式时间戳的任何更新都会导致当前时间戳值的更新.

in it. DEFAULT CURRENT_TIMESTAMP means that any INSERT without an explicit time stamp setting uses the current time. Likewise, ON UPDATE CURRENT_TIMESTAMP means that any update without an explicit timestamp results in an update to the current timestamp value.

您可以在创建表时控制此默认行为.

You can control this default behavior when creating your table.

或者,如果首先没有正确创建时间戳列,则可以对其进行更改.

Or, if the timestamp column wasn't created correctly in the first place, you can change it.

ALTER TABLE whatevertable
     CHANGE whatevercolumn 
            whatevercolumn TIMESTAMP NOT NULL
                           DEFAULT CURRENT_TIMESTAMP 
                           ON UPDATE CURRENT_TIMESTAMP;

这将导致对表的INSERT和UPDATE操作自动更新您的时间戳列.如果要更新whatevertable而不更改时间戳,即

This will cause both INSERT and UPDATE operations on the table automatically to update your timestamp column. If you want to update whatevertable without changing the timestamp, that is,

要防止在其他列更改时更新该列

To prevent the column from updating when other columns change

然后您需要发布这种更新.

then you need to issue this kind of update.

UPDATE whatevertable
   SET something = 'newvalue',
       whatevercolumn = whatevercolumn
 WHERE someindex = 'indexvalue'

而且,这仅适用于TIMESTAMP列,不适用于DATETIMEDATE列.因为这些列是TIMESTAMP,所以要考虑时区:在正确配置的服务器计算机上,这些值始终存储在UTC中,并在检索时转换为本地时间.

And, this works with TIMESTAMP columns only, not DATETIME or DATE columns. Because the columns are TIMESTAMPs, time zones are accounted for: on a correctly configured server machine, those values are always stored in UTC and translated to local time upon retrieval.

这篇关于时间戳记(自动)何时更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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