MySQL创建时间和更新时间时间戳 [英] MySQL create time and update time timestamp
问题描述
我正在创建一些表,我想在这些表中存储创建记录和最后更新记录的时间.我以为我可以有两个时间戳字段,其中一个字段的值为CURRENT_TIMESTAMP
,另一个字段的值为CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
.但是我想我做不到,因为您在表中只能有一个带有默认值的时间戳字段?
I am creating some tables where I want to store the time when a record was created and when it was last updated. I thought I could have two timestamp fields where one would have the value CURRENT_TIMESTAMP
and the other would have CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
. But I guess I can't do this because you can have only 1 timestamp field with a default value in a table?
您如何建议我两次保存两次?谢谢!
How would you recommend I get and store the two times? Thanks!
推荐答案
您可以在一个表中有两列类型为timestamp的列.
You can have two columns of type timestamp in one table.
以下内容适用于MySQL 5.0
The following works for MySQL 5.0
create table t
(
id integer,
created_at timestamp default current_timestamp,
updated_at timestamp
);
我认为您将它与SQL Server混淆(时间戳不是真正的时间戳",并且单个时间戳"列的确存在限制)
I think you are confusing this with SQL Server (where timestamp is not really a "time stamp" and there is indeed a limit on a single "timestamp" column)
但是每次更改行时,您都需要一个触发器来更新update_at列.
But you will need a trigger to update the update_at column each time the row is changed.
这篇关于MySQL创建时间和更新时间时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!