Skip to main content

Questions tagged [insert-update]

A mixed flavour of data entry mode and data modification mode. It can be used when insert is required for case of non-existing data and modification if data exists.

insert-update
1171 votes
13 answers
1.3m views

Insert into a MySQL table or update if exists

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_name (ID, NAME, AGE) VALUES(1, "A", 19); Let’s ...
Keshan's user avatar
  • 14.6k
396 votes
7 answers
393k views

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

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 ...
Craig Ringer's user avatar
165 votes
8 answers
443k views

SQL Server insert if not exists best practice [closed]

I have a Competitions results table which holds team member's names and their ranking on one hand. On the other hand I need to maintain a table of unique competitors names: CREATE TABLE Competitors (...
Didier Levy's user avatar
  • 3,433
158 votes
2 answers
66k views

How do I update if exists, insert if not (AKA "upsert" or "merge") in MySQL?

Is there an easy way to INSERT a row when it does not exist, or to UPDATE if it exists, using one MySQL query?
blub's user avatar
  • 2,166
151 votes
4 answers
181k views

INSERT INTO ... SELECT FROM ... ON DUPLICATE KEY UPDATE

I'm doing an insert query where most of many columns would need to be updated to the new values if a unique key already existed. It goes something like this: INSERT INTO lee(exp_id, created_by, ...
dnagirl's user avatar
  • 20.4k
113 votes
2 answers
34k views

Why are 2 rows affected in my `INSERT ... ON DUPLICATE KEY UPDATE`?

I'm doing an INSERT ... ON DUPLICATE KEY UPDATE for a PRIMARY KEY in the following table: DESCRIBE users_interests; +------------+---------------------------------+------+-----+---------+-------+ | ...
Josh Smith's user avatar
  • 14.9k
87 votes
5 answers
264k views

Update a column in MySQL

I have a table table1 with three columns and a bunch of rows: [key_col|col_a|col_b] I want to update col_a with a set of values (i.e. leaving col_b unchanged), something like this: INSERT INTO ...
Muleskinner's user avatar
  • 14.4k
77 votes
4 answers
155k views

SQLite "INSERT OR REPLACE INTO" vs. "UPDATE ... WHERE"

I've never seen the syntax INSERT OR REPLACE INTO names (id, name) VALUES (1, "John") used in SQL before, and I was wondering why it's better than UPDATE names SET name = "John" WHERE id = 1. Is there ...
nevan king's user avatar
  • 113k
68 votes
11 answers
38k views

.NET Dictionary: get existing value or create and add new value

I often find myself creating a Dictionary with a non-trivial value class (e.g. List), and then always writing the same code pattern when filling in data. For example: var dict = new Dictionary<...
Rok Strniša's user avatar
  • 7,013
49 votes
7 answers
26k views

Is there a way to do an "INSERT...ON DUPLICATE KEY UPDATE" in Zend Framework 1.5?

I would like to use ON DUPLICATE KEY UPDATE in Zend Framework 1.5, is this possible? Example INSERT INTO sometable (...) VALUES (...) ON DUPLICATE KEY UPDATE ...
danielrsmith's user avatar
  • 4,060
47 votes
5 answers
38k views

T-SQL Insert or update

I have a question regarding performance of SQL Server. Suppose I have a table persons with the following columns: id, name, surname. Now, I want to insert a new row in this table. The rule is the ...
Markus's user avatar
  • 3,627
46 votes
3 answers
52k views

How to update elements within a heap? (priority queue)

When using a min/max-heap algorithm, priorities may change. One way to handle this is to removal and insert the element to update the queue order. For priority queues implemented using arrays, this ...
ideasman42's user avatar
  • 46.3k
44 votes
5 answers
72k views

Does DB2 have an "insert or update" statement?

From my code (Java) I want to ensure that a row exists in the database (DB2) after my code is executed. My code now does a select and if no result is returned it does an insert. I really don't like ...
Mikael Eriksson's user avatar
35 votes
6 answers
62k views

How to INSERT a record or UPDATE if it already exists?

I have a table with columns record_id (auto inc), sender, sent_time and status. In case there isn't any record of a particular sender, for example "sender1", I have to INSERT a new record otherwise I ...
Niko Gamulin's user avatar
  • 66.4k
30 votes
3 answers
32k views

Django - save() update on duplicate key

I have little application which allows a user to rate a video. The user can rate only once. So I have defined the uniqueness on the model. But he should be able change his rate. So the save() should ...
Pierre de LESPINAY's user avatar

15 30 50 per page
1
2 3 4 5
88