MySQL自动存储每行的日期时间
在 MySQL 中,我厌倦了将列 dt_created
和 dt_modified
(分别是创建和最后修改的日期时间戳)添加到数据库中的所有表中。
每次INSERT
或UPDATE
数据库时,我都必须使用NOW()
关键字。这一切都贯穿了我的坚持。
是否有任何有效的替代方案,MySQL 可以自动存储至少插入行的数据时间并让我检索它?
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.
Every time I INSERT
or UPDATE
the database, I will have to use the NOW()
keyword. This is going all over my persistence.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(7)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您可以使用 DEFAULT 约束来设置时间戳:
这样您就不必在 INSERT/UPDATE 语句中指定
NOW()
。参考: TIMESTAMP 属性
You can use DEFAULT constraints to set the timestamp:
Then you wouldn't have to specify
NOW()
in your INSERT/UPDATE statements.Reference: TIMESTAMP properties