SQLite - UPSERT *not* INSERT or REPLACE

后端 未结 18 2587
猫巷女王i
猫巷女王i 2020-11-21 23:53

http://en.wikipedia.org/wiki/Upsert

Insert Update stored proc on SQL Server

Is there some clever way to do this in SQLite that I have not thought of?

相关标签:
18条回答
  • 2020-11-22 00:17

    I realize this is an old thread but I've been working in sqlite3 as of late and came up with this method which better suited my needs of dynamically generating parameterized queries:

    insert or ignore into <table>(<primaryKey>, <column1>, <column2>, ...) values(<primaryKeyValue>, <value1>, <value2>, ...); 
    update <table> set <column1>=<value1>, <column2>=<value2>, ... where changes()=0 and <primaryKey>=<primaryKeyValue>; 
    

    It's still 2 queries with a where clause on the update but seems to do the trick. I also have this vision in my head that sqlite can optimize away the update statement entirely if the call to changes() is greater than zero. Whether or not it actually does that is beyond my knowledge, but a man can dream can't he? ;)

    For bonus points you can append this line which returns you the id of the row whether it be a newly inserted row or an existing row.

    select case changes() WHEN 0 THEN last_insert_rowid() else <primaryKeyValue> end;
    
    0 讨论(0)
  • Expanding on Aristotle’s answer you can SELECT from a dummy 'singleton' table (a table of your own creation with a single row). This avoids some duplication.

    I've also kept the example portable across MySQL and SQLite and used a 'date_added' column as an example of how you could set a column only the first time.

     REPLACE INTO page (
       id,
       name,
       title,
       content,
       author,
       date_added)
     SELECT
       old.id,
       "about",
       "About this site",
       old.content,
       42,
       IFNULL(old.date_added,"21/05/2013")
     FROM singleton
     LEFT JOIN page AS old ON old.name = "about";
    
    0 讨论(0)
  • 2020-11-22 00:21

    Beginning with version 3.24.0 UPSERT is supported by SQLite.

    From the documentation:

    UPSERT is a special syntax addition to INSERT that causes the INSERT to behave as an UPDATE or a no-op if the INSERT would violate a uniqueness constraint. UPSERT is not standard SQL. UPSERT in SQLite follows the syntax established by PostgreSQL. UPSERT syntax was added to SQLite with version 3.24.0 (pending).

    An UPSERT is an ordinary INSERT statement that is followed by the special ON CONFLICT clause

    Image source: https://www.sqlite.org/images/syntax/upsert-clause.gif

    0 讨论(0)
  • 2020-11-22 00:26

    Here is a solution that really is an UPSERT (UPDATE or INSERT) instead of an INSERT OR REPLACE (which works differently in many situations).

    It works like this:
    1. Try to update if a record with the same Id exists.
    2. If the update did not change any rows (NOT EXISTS(SELECT changes() AS change FROM Contact WHERE change <> 0)), then insert the record.

    So either an existing record was updated or an insert will be performed.

    The important detail is to use the changes() SQL function to check if the update statement hit any existing records and only perform the insert statement if it did not hit any record.

    One thing to mention is that the changes() function does not return changes performed by lower-level triggers (see http://sqlite.org/lang_corefunc.html#changes), so be sure to take that into account.

    Here is the SQL...

    Test update:

    --Create sample table and records (and drop the table if it already exists)
    DROP TABLE IF EXISTS Contact;
    CREATE TABLE [Contact] (
      [Id] INTEGER PRIMARY KEY, 
      [Name] TEXT
    );
    INSERT INTO Contact (Id, Name) VALUES (1, 'Mike');
    INSERT INTO Contact (Id, Name) VALUES (2, 'John');
    
    -- Try to update an existing record
    UPDATE Contact
    SET Name = 'Bob'
    WHERE Id = 2;
    
    -- If no record was changed by the update (meaning no record with the same Id existed), insert the record
    INSERT INTO Contact (Id, Name)
    SELECT 2, 'Bob'
    WHERE NOT EXISTS(SELECT changes() AS change FROM Contact WHERE change <> 0);
    
    --See the result
    SELECT * FROM Contact;
    

    Test insert:

    --Create sample table and records (and drop the table if it already exists)
    DROP TABLE IF EXISTS Contact;
    CREATE TABLE [Contact] (
      [Id] INTEGER PRIMARY KEY, 
      [Name] TEXT
    );
    INSERT INTO Contact (Id, Name) VALUES (1, 'Mike');
    INSERT INTO Contact (Id, Name) VALUES (2, 'John');
    
    -- Try to update an existing record
    UPDATE Contact
    SET Name = 'Bob'
    WHERE Id = 3;
    
    -- If no record was changed by the update (meaning no record with the same Id existed), insert the record
    INSERT INTO Contact (Id, Name)
    SELECT 3, 'Bob'
    WHERE NOT EXISTS(SELECT changes() AS change FROM Contact WHERE change <> 0);
    
    --See the result
    SELECT * FROM Contact;
    
    0 讨论(0)
  • 2020-11-22 00:28

    If you don't mind doing this in two operations.

    Steps:

    1) Add new items with "INSERT OR IGNORE"

    2) Update existing items with "UPDATE"

    The input to both steps is the same collection of new or update-able items. Works fine with existing items that need no changes. They will be updated, but with the same data and therefore net result is no changes.

    Sure, slower, etc. Inefficient. Yep.

    Easy to write the sql and maintain and understand it? Definitely.

    It's a trade-off to consider. Works great for small upserts. Works great for those that don't mind sacrificing efficiency for code maintainability.

    0 讨论(0)
  • 2020-11-22 00:30

    INSERT OR REPLACE is NOT equivalent to "UPSERT".

    Say I have the table Employee with the fields id, name, and role:

    INSERT OR REPLACE INTO Employee ("id", "name", "role") VALUES (1, "John Foo", "CEO")
    INSERT OR REPLACE INTO Employee ("id", "role") VALUES (1, "code monkey")
    

    Boom, you've lost the name of the employee number 1. SQLite has replaced it with a default value.

    The expected output of an UPSERT would be to change the role and to keep the name.

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