Search code examples
mysqlnode-mysql

How to insert two datetime fields with same server time in mysql?


I have a table with two fields insert_time and update_time. The type of insert_time is varchar(30) NOT NULL DEFAULT '' while the type of update_time is timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.

insert into tableName(inser_time,update_time) values('2017-02-17 20:30:38')

will make the update_time lose its meanings.

So how to make the two fields have a same server time when inserted if not update the Mysql version?


Solution

  • Just set both fields to the same value ?

    insert into tableName(inser_time,update_time) 
    values('2017-02-17 20:30:38','2017-02-17 20:30:38')
    

    EDIT to use current time

    insert into tableName(inser_time,update_time) 
    values(now(), now())
    

    EDIT 2 From mysql manual at: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_now

    Functions that return the current date or time each are evaluated only once per query at the start of query execution. This means that multiple references to a function such as NOW() within a single query always produce the same result.