luosuosile
V2EX  ›  问与答

mysql 数据库怎么实现时间自动更新

  •  
  •   luosuosile · Dec 3, 2018 · 3157 views
    This topic created in 2732 days ago, the information mentioned may be changed or developed.

    最近在设计数据库,看阿里云开发手册 [强制] 表必备三字段:id, gmt_create, gmt_modified。 说明:其中 id 必为主键,类型为 unsigned bigint、单表时自增、步长为 1。gmt_create, gmt_modified 的类型均为 date_time 类型,前者现在时表示主动创建,后者过去分词表示被动更新。 但是 mysql datetime 似乎不能实现自动更新和默认当前时间。 timestamp 也只能有一个可以设置默认时间和当前时间戳更新。

    2 replies    2018-12-04 09:40:55 +08:00
    xiaoxinshiwo
        1
    xiaoxinshiwo  
       Dec 3, 2018
    道理你不都说清楚了吗?
    gmt_modified CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    gmt_create datetime
    luosuosile
        2
    luosuosile  
    OP
       Dec 4, 2018
    在 MySQL 5.6.5 版本之前,Automatic Initialization and Updating 只适用于 TIMESTAMP,而且一张表中,最多允许一个 TIMESTAMP 字段采用该特性。从 MySQL 5.6.5 开始,Automatic Initialization and Updating 同时适用于 TIMESTAMP 和 DATETIME,且不限制数量。
    道理出在这里吧,,我那疯狂限制数量,datetime 也不许我用
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4386 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 00:10 · PVG 08:10 · LAX 17:10 · JFK 20:10
    ♥ Do have faith in what you're doing.