MySQL Workbench:如何设置“更新时”和 CURRENT_TIMESTAMP?

发布于 2024-12-17 11:10:30 字数 726 浏览 6 评论 0原文

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

我正在尝试在工作台中对类似的东西进行建模,但我不知道在哪里设置“更新时”部分。我能得到的最好结果如下:

-- -----------------------------------------------------
-- 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;

我应该在工作台的哪里设置这个更新部分?

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

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:

-- -----------------------------------------------------
-- 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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

纸伞微斜 2024-12-24 11:10:30

我正在使用 MySQL Workbench 5.2.35。打开创建/更改表面板,切换到列选项卡,右键单击时间戳字段;在那里您可以看到可能的默认更新选项。

重要提示:您可以使用 CURRENT_TIMESTAMP 作为表中单个列的默认值或更新值!

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

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

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

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 default and on update options.

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

Sample screenshot showing the context menu

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

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

风吹短裙飘 2024-12-24 11:10:30

执行的操作

这是我使用 MySQL Workbench数据类型:TIMESTAMP
默认值:更新 CURRENT_TIMESTAMP 时的 CURRENT_TIMESTAMP

Here is what I do with MySQL Workbench

Data Type: TIMESTAMP
Default: CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

愿与i 2024-12-24 11:10:30

默认情况下,MySQL 在每个 UPDATEINSERT 语句上将第一个 timestamp 字段设置为 CURRENT_TIMESTAMP。我以某种方式设计我的架构,使 updated_at 成为第一个 timestamp 字段,这样我就不必显式指定它。

By default MySQL sets the first timestamp field to CURRENT_TIMESTAMP on every UPDATE or INSERT statement. I go about designing my schema in a way so that updated_at is first timestamp field, so that I don't have to explicitly specify it.

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