MySQL 时间戳仅在创建时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/817396/
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 13:13:07  来源:igfitidea点击:

MySQL timestamp only on create

mysqltimestamp

提问by Nir

I use timestamp on MySQL 5.x (with PHP) to remember event times. During development I had to update the table with a query that changes something in all columns. The timestamp was then reset to current time.

我在 MySQL 5.x(使用 PHP)上使用时间戳来记住事件时间。在开发过程中,我不得不使用更改所有列中某些内容的查询来更新表。然后时间戳被重置为当前时间。

How can I make timestamp change only on inserts and not on updates or replace?

如何仅对插入而不是更新或替换进行时间戳更改?

回答by Paolo Bergantino

Here's all you need to know. In short, though, I think this should do it:

这是您需要知道的一切。总之,我认为应该这样做:

ALTER TABLE `mytable`
CHANGE `mydatefield` `mydatefield`
TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

回答by cherouvim

You can use a default valuefor that field and not include it in the insert or update query.

您可以为该字段使用默认值,而不将其包含在插入或更新查询中。

回答by user99974

ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP

Very good documentation here for time-stamp.

非常好的时间戳文档。