MySQL 时间戳仅在创建时

2022-01-13 00:00:00 timestamp mysql

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

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.

如何仅在插入时更改时间戳而不在更新或替换时更改?

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

推荐答案

这里有你需要知道的一切.简而言之,我认为应该这样做:

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

相关文章