MySQL ON UPDATE CURRENT_TIMESTAMP not updating

前端 未结 5 1809
轮回少年
轮回少年 2020-12-24 13:06

I\'ve got a table that looks like this:

CREATE TABLE IF NOT EXISTS `Hosts` (
`id` int(128) NOT NULL AUTO_INCREMENT,
`IP` varchar(15) NOT NULL DEFAULT \'\',
`         


        
相关标签:
5条回答
  • 2020-12-24 13:33

    To specify automatic properties, use the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses. The order of the clauses does not matter. If both are present in a column definition, either can occur first. Any of the synonyms for CURRENT_TIMESTAMP have the same meaning as CURRENT_TIMESTAMP. These are CURRENT_TIMESTAMP(), NOW(), LOCALTIME, LOCALTIME(), LOCALTIMESTAMP, and LOCALTIMESTAMP().

    Use of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is specific to TIMESTAMP. The DEFAULT clause also can be used to specify a constant (nonautomatic) default value; for example, DEFAULT 0 or DEFAULT '2000-01-01 00:00:00'.

    DEFAULT 0 do not work if the NO_ZERO_DATE SQL mode is enabled because that mode causes “zero” date values (specified, for example, as 0 '0000-00-00 00:00:00') to be rejected. Be aware that the TRADITIONAL SQL mode includes NO_ZERO_DATE.

    In addition, you can initialize or update any TIMESTAMP column to the current date and time by assigning it a NULL value, unless it has been defined with the NULL attribute to permit NULL values.

    0 讨论(0)
  • 2020-12-24 13:34

    Have you tried to use null for that field when updating?

    You could also try setting default value to CURRENT_TIMESTAMP, rather than 0000-00-00 00:00:00.

    Nevertheless, whenever I want to have creation and update time I always use the following:

    ...
    CREATED timestamp NOT NULL default '0000-00-00 00:00:00',
    UPDATED timestamp NOT NULL default now() on update now(),
    ....
    

    I use now(), because is an alias for CURRENT_TIMESTAMP and it is shorter. At the end, table structure gets CURRENT_TIMESTAMP, so don't worry.

    The trick with CREATED field is to remember to use null on both fields for INSERT statements, for UPDATE statements it is not required:

    INSERT INTO mytable (field1, field2, created, updated)
    VALUES ('foo', 'bar', null, null);
    
    0 讨论(0)
  • 2020-12-24 13:34

    You must remember that if no value was changed on update it won't set the current time stamp,

    You have to set the value in query NOW() to set for the current time stamp !!!

    update Hosts set Backupstatus = 'FAIL',Lastconnection = NOW() , Backupmsg = 'Connection timed out' where Tid = 'SITE001'

    Remember the value must change in order for the current time stamp to change.

    0 讨论(0)
  • 2020-12-24 13:42

    It might be the case that the update statement doesn't change anything. If the row with Tid = 'SITE001' already has Backupstatus set to 'FAIL' and Backupmsg set to 'Connection timed out' (maybe, set by some previous backup attempt), then MySQL will skip this row and therefore won't change the Lastconnection timestamp.

    Also, I see ON UPDATE CURRENT_TIMESTAMP more like an administrative feature to keep track of data changes. As a programmer, I would add the timestamp update explicitly:

    update Hosts
    set Backupstatus = 'FAIL', Backupmsg = 'Connection timed out', Lastconnection = NOW() where Tid = 'SITE001'
    .

    0 讨论(0)
  • 2020-12-24 13:59

    If you want the record to automatically update the timestamp whenever the record is changed, here's the four simple steps you need to accomplish (could be all in one step, depending on if you're using command line or GUI to administer):

    1. Create field to hold auto-updated timestamp (I typically call mine 'modified').
    2. Specify field Type as 'TIMESTAMP'
    3. Specify field Default as 'CURRENT_TIMESTAMP'
    4. Specify field Extra as 'ON UPDATE CURRENT_TIMESTAMP'

    Now the field that contains your timestamp will always be updated to the current timestamp anytime the record is updated.

    0 讨论(0)
提交回复
热议问题