MySQL auto-store datetime for each row(MySQL自动存储每一行的日期时间)
问题描述
在 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 CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
那么您就不必在 INSERT/UPDATE 语句中指定 NOW().
Then you wouldn't have to specify NOW() in your INSERT/UPDATE statements.
参考:TIMESTAMP 属性
这篇关于MySQL自动存储每一行的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL自动存储每一行的日期时间
基础教程推荐
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 无法解决整理冲突 2021-01-01
