MySQL自动存储每行的日期时间

发布于 2024-08-15 14:39:37 字数 255 浏览 4 评论 0原文

在 MySQL 中,我厌倦了将列 dt_createddt_modified (分别是创建和最后修改的日期时间戳)添加到数据库中的所有表中。

每次INSERTUPDATE 数据库时,我都必须使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

路还长,别太狂 2024-08-22 14:39:37

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

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()

参考: TIMESTAMP 属性

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

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

Reference: TIMESTAMP properties

把人绕傻吧 2024-08-22 14:39:37

如果您使用 phpmyadmin,您可以通过以下方式执行此操作:

在此处输入图像描述

If you're using phpmyadmin you can do this by :

enter image description here

猥︴琐丶欲为 2024-08-22 14:39:37
ALTER TABLE  `tablename` CHANGE  `dt`  `dt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

应该是正确的代码。

ALTER TABLE  `tablename` CHANGE  `dt`  `dt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

should be the correct code.

゛时过境迁 2024-08-22 14:39:37

好吧,你不能两者兼得:

mysql doc:

不可能将当前时间戳设为一列的默认值而将另一列设为自动更新值。

很难过,不是吗?

但是,您可以使用 null 代替 now() 按照此提示< /a>

Well, you can't have both:

mysql doc:

It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.

Sad, isn't it?

You could however use null instead of now() following this tip

泪冰清 2024-08-22 14:39:37

这里提出了类似的问题“Timestamp for MySQL”时间戳字段将在每次访问时更新。您还可以考虑在相关表上放置一个 触发器自动为您填充这些字段。根据环境的不同,有些商店/企业不喜欢使用触发器,因此您可能必须找到替代的解决方法。

Similar question was asked here "Timestamp for MySQL" the timestamp field will update every time it is accessed. You might also consider a Trigger placed on the table in question to automatically populate those fields for you. Depending on the environment some shops/businesses do not like the use of triggers and so you might have to find alternate work arounds.

甜`诱少女 2024-08-22 14:39:37

在 phpmyadmin 中你可以设置
在此处输入图像描述


使用此查询

ALTER TABLE  `tablename`
    CHANGE  `dt_created`  `dt_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

In phpmyadmin you can set
enter image description here

OR
use this query

ALTER TABLE  `tablename`
    CHANGE  `dt_created`  `dt_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
木格 2024-08-22 14:39:37

可以设置为默认行更新

ALTER TABLE `tablename` CHANGE `dt` `dt` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;

could be set as default an on update of rows

ALTER TABLE `tablename` CHANGE `dt` `dt` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文