MySQL 更新条目而不更新时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2844863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:03:27  来源:igfitidea点击:

Updating entry WITHOUT updating timestamp

mysqlsqltimestamp

提问by Andy

I have a timestamp in a mysql table with attribute "ON UPDATE CURRENT_TIMESTAMP". Is there a way to manually disable updating the timestamp on a special occasion? (eg: updating the entry to revise a blog post, but not to re-date it)

我在 mysql 表中有一个时间戳,其属性为“ON UPDATE CURRENT_TIMESTAMP”。有没有办法在特殊情况下手动禁用更新时间戳?(例如:更新条目以修改博客文章,但不更新它的日期)

采纳答案by OMG Ponies

Is there a way to manually disable updating the timestamp on a special occasion? (eg: updating the entry to revise a blog post, but not to re-date it)

有没有办法在特殊情况下手动禁用更新时间戳?(例如:更新条目以修改博客文章,但不重新日期)

Sounds like you need to configure the default constraint so that it populates the column on insertion only:

听起来您需要配置默认约束,以便它仅在插入时填充列:

DEFAULT CURRENT_TIMESTAMP

Changing it to only be this means that any revisions will not trigger the timestamp value to be updated. IE: If you created the blogpost yesterday, and corrected a typo today - the date in the column would still be for yesterday.

将其更改为仅此意味着任何修订都不会触发要更新的时间戳值。IE:如果您昨天创建了博文,并在今天更正了一个错字 - 列中的日期仍然是昨天。

回答by Andy

You can manually set the value of the column to its current value in the update command:

您可以在更新命令中手动将列的值设置为其当前值:

UPDATE table SET x=y, timestampColumn=timestampColumn WHERE a=b;

If you don't set the value in the query, it will be updated to the current timestamp as per the table definition.

如果您未在查询中设置该值,它将根据表定义更新为当前时间戳。

回答by Olie

To make your table/timestamp auto-update:

要使您的表/时间戳自动更新:

ALTER TABLE myTable
CHANGE myTimestampColumn
        myTimestampColumn TIMESTAMP NOT NULL
                       DEFAULT CURRENT_TIMESTAMP 
                       ON UPDATE CURRENT_TIMESTAMP;

To make it notauto-update:

要使其自动更新:

ALTER TABLE myTable
CHANGE myTimestampColumn
        myTimestampColumn TIMESTAMP NOT NULL
                       DEFAULT CURRENT_TIMESTAMP;

NOTE: The "default current_timestamp" part just sets it to the current stamp at default time, since the field is not-null. You can remove both the not null and the default, if you like.

注意:“默认 current_timestamp”部分只是将其设置为默认时间的当前标记,因为该字段不为空。如果您愿意,您可以同时删除非空值和默认值。

回答by knittl

don't use timestamps, but track times manually.

不要使用时间戳,而是手动跟踪时间。

if you really want to update a record and don't update it's timestamp use:

如果您真的想更新记录而不更新它的时间戳,请使用:

UPDATE `table` SET `timestamp` = `timestamp`, `col` = 'new data' …;

回答by Pini Cheyni

If you do change timestemp on update then you have to take into your consideration that if the value was updated but not changed (updated the save value) then it will not update the "on update Current_timestemp" and on this situation you should set the timestemp manually

如果您确实在更新时更改了时间,那么您必须考虑到如果值已更新但未更改(更新保存值),则它不会更新“更新 Current_timestemp”,在这种情况下,您应该设置时间手动

SET LastUpdatedDate=NOW() WHERE

The idea came from here: Touch MYSQL record to update TIMESTAMP field

这个想法来自这里: Touch MYSQL record to update TIMESTAMP field