MySQL Workbench:如何设置“ON UPDATE”和 CURRENT_TIMESTAMP?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8194157/
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 21:46:04  来源:igfitidea点击:

MySQL Workbench: how do I set up "ON UPDATE" and CURRENT_TIMESTAMP?

mysqltimestamputcmysql-workbench

提问by StackOverflowNewbie

SEE: Update timestamp column in Application or Database?

请参阅:更新应用程序或数据库中的时间戳列?

I'm trying to model something similar in Workbench, but I don't know where to set the "ON UPDATE" part. The best I can get is the following:

我正在尝试在 Workbench 中模拟类似的东西,但我不知道在哪里设置“ON UPDATE”部分。我能得到的最好的是以下内容:

-- -----------------------------------------------------
-- Table `foo`.`test`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `foo`.`test` ;

CREATE  TABLE IF NOT EXISTS `foo`.`test` (
  `test_id` INT NOT NULL ,
  `date_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
  `date_updated` TIMESTAMP NOT NULL DEFAULT 0 ,
  PRIMARY KEY (`test_id`) )
ENGINE = InnoDB;

Where do I go in Workbench to set up this ON UPDATE part?

我在 Workbench 中的何处设置此 ON UPDATE 部分?

Also, I have a rule that all timestamps stored in the database should be UTC. How do I make CURRENT_TIMESTAMP, NOW, etc. be UTC?

另外,我有一个规则,即存储在数据库中的所有时间戳都应该是 UTC。如何使 CURRENT_TIMESTAMP、NOW 等成为 UTC?

回答by melihcelik

I am using MySQL Workbench 5.2.35. Open create/alter table panel, switch to the columns tab, right click on the timestamp field; there you can see possible defaultand on updateoptions.

我正在使用 MySQL Workbench 5.2.35。打开create/alter table面板,切换到columns选项卡,右键timestamp字段;在那里你可以看到可能defaulton update选项。

Important note: You can use CURRENT_TIMESTAMPas default or updated value for only a single column in a table!

重要说明:您CURRENT_TIMESTAMP只能将表中的单个列用作默认值或更新值!

Sample screenshot showing the context menu

显示上下文菜单的示例屏幕截图

Regarding the UTC question, you can have a look at this question. There is an accepted solution there.

关于UTC问题,你可以看看这个问题。那里有一个公认的解决方案。

I would suggest you to read MySQL reference manuals as well for Timestamp data typeand NOW()function.

我建议您阅读 MySQL 参考手册以及Timestamp 数据类型NOW()函数

回答by Cantika Onies

Here is what I do with MySQL Workbench

这是我用 MySQL Workbench 做的事情

Data Type: TIMESTAMP Default: CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

数据类型:TIMESTAMP 默认值:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

回答by Ashwini Dhekane

By default MySQL sets the first timestampfield to CURRENT_TIMESTAMPon every UPDATEor INSERTstatement. I go about designing my schema in a way so that updated_atis first timestampfield, so that I don't have to explicitly specify it.

默认情况下,MySQL 将第一个timestamp字段设置为CURRENT_TIMESTAMPon 每个UPDATEorINSERT语句。我以某种方式设计我的模式,以便它updated_at是第一个timestamp字段,这样我就不必明确指定它。