MySQL自动存储每行的datetime [英] MySQL auto-store datetime for each row

查看:199
本文介绍了MySQL自动存储每行的datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MySQL中,我不得不添加列 dt_created dt_modified (这些日期时间戳为创建和最后修改)给我在我的数据库中的所有表。

In MySQL, I'm sick of adding the columns dt_created and dt_modified (which are date time stamps for creation and last modified respectively) to all the tables I have in my database.

每次我 INSERT UPDATE 数据库,我将不得不使用 NOW()关键字。这完全是我的坚持。

Every time I INSERT or UPDATE the database, I will have to use the NOW() keyword. This is going all over my persistence.

有没有什么有效的选择,MySQL可以自动存储至少插入行的数据时间,并让我检索它?

Is there any efficient alternative where MySQL can automatically store at least the datatime of the row that is inserted and let me retrieve it?

推荐答案

您可以使用DEFAULT约束来设置时间戳:

You can use DEFAULT constraints to set the timestamp:

ALTER TABLE
 MODIFY dt_created datetime DEFAULT CURRENT_TIMESTAMP

ALTER TABLE
 MODIFY dt_modified datetime DEFAULT ON UPDATE CURRENT_TIMESTAMP

然后,您不必在INSERT / UPDATE语句中指定 NOW()

Then you wouldn't have to specify NOW() in your INSERT/UPDATE statements.

参考: TIMESTAMP属性

这篇关于MySQL自动存储每行的datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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