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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1932093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:50:17  来源:igfitidea点击:

MySQL auto-store datetime for each row

sqlmysqldatetimeaudit

提问by mauris

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

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

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

每次我INSERTUPDATE数据库时,我都必须使用NOW()关键字。这贯穿了我的坚持。

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?

是否有任何有效的替代方法可以让 MySQL 至少自动存储插入行的数据时间并让我检索它?

回答by OMG Ponies

You can use DEFAULT constraints to set the timestamp:

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

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.

那么您就不必NOW()在 INSERT/UPDATE 语句中指定。

Reference: TIMESTAMP properties

参考:TIMESTAMP 属性

回答by Ouadie

If you're using phpmyadminyou can do this by :

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

enter image description here

在此处输入图片说明

回答by Kim Stacks

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

should be the correct code.

应该是正确的代码。

回答by ggd

Well, you can't have both:

好吧,你不能同时拥有:

mysql doc:

mysql 文档:

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

但是,您可以按照此提示使用 null 而不是 now()

回答by GrayWizardx

Similar question was asked here "Timestamp for MySQL" the timestamp field will update every time it is accessed. You might also consider a Triggerplaced 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.

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

回答by user3110005

In phpmyadmin you can set enter image description here

在 phpmyadmin 中你可以设置 在此处输入图片说明

OR use this query

或使用此查询

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

回答by Jan Mark

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;