【问题标题】:mysql insert timestamp and update timestampmysql插入时间戳和更新时间戳
【发布时间】:2015-06-12 02:21:02
【问题描述】:

我们在创建 MySQL 表时,通常会添加列 insert_timestampupdate_timestamp 来节省插入时间和更新时间,我们可以设置 update_timestamp DEFAULT CURRENT_TIMESTAMP,update_timestamp更新一行就更新timestamp,但是insert_time怎么设置为插入的时间呢?

【问题讨论】:

    标签: mysql insert-update


    【解决方案1】:

    您可以使用插入语句“手动”设置它,或者让数据库使用触发器来设置。

    【讨论】:

      【解决方案2】:

      您可以像这样通过插入查询传递它:

      "INSERT INTO table_name (column1, column2, column3, insert_timestamp)
      VALUES (value1, value2, value3, insertTime)"
      

      insertTime 将替换为您的本地时间。另一种选择是为您的表创建一个 before_insert 触发器,例如:

      delimiter //
      CREATE TRIGGER bi_tableName BEFORE INSERT ON tableName
      FOR EACH ROW
      BEGIN
          SET NEW.insert_timestamp = NOW();
      END;//
      

      见:https://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html, http://dev.mysql.com/doc/refman/5.6/en/insert.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-19
        • 1970-01-01
        • 2015-08-01
        • 2011-03-30
        • 2010-09-18
        • 1970-01-01
        相关资源
        最近更新 更多