upsert

How to use RETURNING with ON CONFLICT in PostgreSQL?

穿精又带淫゛_ 提交于 2019-11-26 01:57:36
问题 I have the following UPSERT in PostgreSQL 9.5: INSERT INTO chats (\"user\", \"contact\", \"name\") VALUES ($1, $2, $3), ($2, $1, NULL) ON CONFLICT(\"user\", \"contact\") DO NOTHING RETURNING id; If there are no conflicts it returns something like this: ---------- | id | ---------- 1 | 50 | ---------- 2 | 51 | ---------- But if there are conflicts it doesn\'t return any rows: ---------- | id | ---------- I want to return the new id columns if there are no conflicts or return the existing id

Solutions for INSERT OR UPDATE on SQL Server

半世苍凉 提交于 2019-11-26 01:18:47
问题 Assume a table structure of MyTable(KEY, datafield1, datafield2...) . Often I want to either update an existing record, or insert a new record if it doesn\'t exist. Essentially: IF (key exists) run update command ELSE run insert command What\'s the best performing way to write this? 回答1: don't forget about transactions. Performance is good, but simple (IF EXISTS..) approach is very dangerous. When multiple threads will try to perform Insert-or-update you can easily get primary key violation.

How to Perform an UPSERT so that I can use both new and old values in update part

吃可爱长大的小学妹 提交于 2019-11-26 01:16:10
问题 Stupid but simple example: Assume I have a table \'Item\' where I keeps totals of the items that receive. Item_Name Items_In_Stock Item name is primary key here. How to i achieve the following when ever I receive item A in quantity X. If the item does not exist, I insert a new recored for Item A and set the items in stock to X and if there exists a record where items in stock was Y then the new value in items in stock is (X + Y) INSERT INTO `item` (`item_name`, items_in_stock) VALUES( \'A\',

MongoDB: upsert sub-document

本秂侑毒 提交于 2019-11-26 00:55:10
问题 I have documents that looks something like that, with a unique index on bars.name : { name: \'foo\', bars: [ { name: \'qux\', somefield: 1 } ] } . I want to either update the sub-document where { name: \'foo\', \'bars.name\': \'qux\' } and $set: { \'bars.$.somefield\': 2 } , or create a new sub-document with { name: \'qux\', somefield: 2 } under { name: \'foo\' } . Is it possible to do this using a single query with upsert, or will I have to issue two separate ones? Related: 'upsert' in an

SQLite INSERT - ON DUPLICATE KEY UPDATE (UPSERT)

断了今生、忘了曾经 提交于 2019-11-26 00:47:11
问题 MySQL has something like this: INSERT INTO visits (ip, hits) VALUES (\'127.0.0.1\', 1) ON DUPLICATE KEY UPDATE hits = hits + 1; As far as I know this feature doesn\'t exist in SQLite, what I want to know is if there is any way to achive the same effect without having to execute two queries. Also, if this is not possible, what do you prefer: SELECT + (INSERT or UPDATE) or UPDATE (+ INSERT if UPDATE fails ) 回答1: Since 3.24.0 SQLite also supports upsert, so now you can simply write the following

Oracle: how to UPSERT (update or insert into a table?)

馋奶兔 提交于 2019-11-25 23:15:52
问题 The UPSERT operation either updates or inserts a row in a table, depending if the table already has a row that matches the data: if table t has a row exists that has key X: update t set mystuff... where mykey=X else insert into t mystuff... Since Oracle doesn\'t have a specific UPSERT statement, what\'s the best way to do this? 回答1: An alternative to MERGE (the "old fashioned way"): begin insert into t (mykey, mystuff) values ('X', 123); exception when dup_val_on_index then update t set

Is SELECT or INSERT in a function prone to race conditions?

一笑奈何 提交于 2019-11-25 22:47:41
问题 I wrote a function to create posts for a simple blogging engine: CREATE FUNCTION CreatePost(VARCHAR, TEXT, VARCHAR[]) RETURNS INTEGER AS $$ DECLARE InsertedPostId INTEGER; TagName VARCHAR; BEGIN INSERT INTO Posts (Title, Body) VALUES ($1, $2) RETURNING Id INTO InsertedPostId; FOREACH TagName IN ARRAY $3 LOOP DECLARE InsertedTagId INTEGER; BEGIN -- I am concerned about this part. BEGIN INSERT INTO Tags (Name) VALUES (TagName) RETURNING Id INTO InsertedTagId; EXCEPTION WHEN UNIQUE_VIOLATION

SQLite - UPSERT *not* INSERT or REPLACE

扶醉桌前 提交于 2019-11-25 22:24:01
问题 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? Basically I want to update three out of four columns if the record exists, If it does not exists I want to INSERT the record with the default (NUL) value for the fourth column. The ID is a primary key so there will only ever be one record to UPSERT. (I am trying to avoid the overhead of SELECT in order to determin if I need to UPDATE or INSERT

How to UPSERT (MERGE, INSERT … ON DUPLICATE UPDATE) in PostgreSQL?

送分小仙女□ 提交于 2019-11-25 21:41:50
问题 A very frequently asked question here is how to do an upsert, which is what MySQL calls INSERT ... ON DUPLICATE UPDATE and the standard supports as part of the MERGE operation. Given that PostgreSQL doesn\'t support it directly (before pg 9.5), how do you do this? Consider the following: CREATE TABLE testtable ( id integer PRIMARY KEY, somedata text NOT NULL ); INSERT INTO testtable (id, somedata) VALUES (1, \'fred\'), (2, \'bob\'); Now imagine that you want to \"upsert\" the tuples (2, \'Joe

Insert into a MySQL table or update if exists

為{幸葍}努か 提交于 2019-11-25 21:39:49
问题 I want to add a row to a database table, but if a row exists with the same unique key I want to update the row. For example, insert into table (id, name, age) values(1, \"A\", 19) Let’s say the unique key is id , and in my database there is a row with id = 1 . In that case I want to update that row with these values. Normally this gives an error. If I use insert IGNORE it will ignore the error, but it still won’t update. 回答1: Use INSERT ... ON DUPLICATE KEY UPDATE QUERY: INSERT INTO table (id