MySQL Workbench:如何设置“更新时”和 CURRENT_TIMESTAMP?
我正在尝试在工作台中对类似的东西进行建模,但我不知道在哪里设置“更新时”部分。我能得到的最好结果如下:
-- -----------------------------------------------------
-- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我正在使用 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
andon update
options.Important note: You can use
CURRENT_TIMESTAMP
as default or updated value for only a single column in a table!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.执行的操作
这是我使用 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
默认情况下,MySQL 在每个
UPDATE
或INSERT
语句上将第一个timestamp
字段设置为CURRENT_TIMESTAMP
。我以某种方式设计我的架构,使updated_at
成为第一个timestamp
字段,这样我就不必显式指定它。By default MySQL sets the first
timestamp
field toCURRENT_TIMESTAMP
on everyUPDATE
orINSERT
statement. I go about designing my schema in a way so thatupdated_at
is firsttimestamp
field, so that I don't have to explicitly specify it.