on update current_timestamp with SQLite

后端 未结 4 514
有刺的猬
有刺的猬 2020-12-04 22:26

I want to update a field with the current timestamp whenever the row is updated.

In MySQL I would do, when declaring the table

LastUpdate TIMESTAMP D         


        
相关标签:
4条回答
  • 2020-12-04 22:29

    There is more efficient, nice and clean way to do it, for example:

    -- List all required fields after 'OF' except the LastUpdate field to prevent infinite loop
    CREATE TRIGGER UpdateLastTime UPDATE OF field1, field2, fieldN ON Package
    BEGIN
      UPDATE Package SET LastUpdate=CURRENT_TIMESTAMP WHERE ActionId=ActionId;
    END;
    

    The code like this one has been tested in my project. Deep sqlite trigger explanation can be found here https://www.sqlite.org/lang_createtrigger.html

    0 讨论(0)
  • 2020-12-04 22:50

    Yes, you'd need to use a trigger. (Just checking: is your posted trigger working correctly? At first glance, it looks fine to me.)

    MySQL's ON UPDATE CURRENT_TIMESTAMP is a pretty unique, single-purpose shortcut. It is what it is; this construct cannot be used similarly for any other values or for any column types other than TIMESTAMP. (Note how this functionality is defined on the TIMESTAMP type page instead of the CREATE TABLE page, as this functionality is specific to TIMESTAMP columns and not CREATE TABLE statements in general.) It's also worth mentioning that while it's specific to a TIMESTAMP type, SQLite doesn't even have distinct date/time types.

    As far as I know, no other RDBMS offers this shortcut in lieu of using an actual trigger. From what I've read, triggers must be used to accomplish this on MS SQL, SQLite, PostgreSQL, and Oracle.


    One last note for passersby:

    This is not to be confused with ON UPDATE clauses in relation to foreign key constraints. That's something entirely different, which likely all RDBMSs that support foreign key constraints have (including both MySQL and SQLite).

    0 讨论(0)
  • 2020-12-04 22:50
    -- Describe UPDATELASTTIME  
    CREATE TRIGGER [UpdateLastTime]  
        AFTER   
        UPDATE  
        ON test
        FOR EACH ROW   
        WHEN NEW.last_update_ts <= OLD.last_update_ts  
    BEGIN  
        update test set last_update_ts=CURRENT_TIMESTAMP where id=OLD.id;  
    END  
    
    -- Describe TEST  
    CREATE TABLE "main"."test" (  
        "id" INTEGER PRIMARY KEY AUTOINCREMENT,  
        "name" TEXT,  
        "last_update_ts" DATETIME DEFAULT CURRENT_TIMESTAMP  
    );  
    
    0 讨论(0)
  • 2020-12-04 22:56

    John is correct about the default SQLite settings, this trigger leads to an infinite loop. To avoid recursion, use the WHEN clause.

    Following will work even if the recursive_triggers setting is on:

    PRAGMA recursive_triggers=1;     --- test
    
    CREATE TRIGGER [UpdateLastTime]
        AFTER UPDATE
        ON package
        FOR EACH ROW
        WHEN NEW.LastUpdate < OLD.LastUpdate    --- this avoid infinite loop
    BEGIN
        UPDATE Package SET LastUpdate=CURRENT_TIMESTAMP WHERE ActionId=OLD.ActionId;
    END;
    
    0 讨论(0)
提交回复
热议问题