Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if when using "on duplicate key update" a row was inserted or updated?

Tags:

mysql

We have a database that gets updated everyday at midnight with a cronjob, we get new data from an external XML.

What we do is that we insert all the new content and in case there is a duplicated key we update that field.

INSERT INTO table (id, col1, col2, col3) values (id_value, val1, val2, val3), (id_value, val1, val2, val3), (id_value, val1, val2, val3), (id_value, val1, val2, val3), ON DUPLICATE KEY UPDATE  col1 = VALUES (col1),  col2 = VALUES (col2),  col3 = VALUES (col3); 

What we want to know is which rows have actually been inserted, meaning we want to have a list of the new items. is there any query that might return the new inserts? Basically we will need to get all the new ID's and not the number of new insertions.

Thanks

like image 602
multimediaxp Avatar asked May 22 '12 20:05

multimediaxp


People also ask

How does on duplicate key update work?

ON DUPLICATE KEY UPDATE is a MariaDB/MySQL extension to the INSERT statement that, if it finds a duplicate unique or primary key, will instead perform an UPDATE. The row/s affected value is reported as 1 if a row is inserted, and 2 if a row is updated, unless the API's CLIENT_FOUND_ROWS flag is set.

How do I use insert on duplicate key update?

INSERT ON DUPLICATE KEY UPDATE statement is available in MySQL as an extension to the INSERT statement. Whenever a new row is inserted into a table in case the row causes a duplicate entry in the UNIQUE index or PRIMARY KEY, MySQL will throw an error.

Is insert on duplicate key update Atomic?

So yes it is atomic in the sense that if the data that you are trying to insert will cause a duplicate in the primary key or in the unique index, the statement will instead perform an update and not error out.

Is insert the same as update?

Insert is for putting in a fresh record to the table. while the update enables you to modify the inserted record e.g. modifying data type etc.


2 Answers

You can get this information at the time of the insert/update by examining the number of affected rows in the result set.

MySQL documentation states:

With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row and 2 if an existing row is updated.

You'll need to combine ROW_COUNT with LAST_INSERT_ID to get your answer and insert one row at a time.

like image 122
Marcus Adams Avatar answered Oct 08 '22 04:10

Marcus Adams


Add an update_count INT NOT NULL DEFAULT 1 column and change your query:

INSERT INTO    table (id, col1, col2, col3) VALUES (id_value, val1, val2, val3), (id_value, val1, val2, val3,), (id_value, val1, val2, val3), (id_value, val1, val2, val3), ON DUPLICATE KEY UPDATE          col1 = VALUES (col1),          col2 = VALUES (col2),          col3 = VALUES (col3),         update_count = update_count + 1; 

You can also increment it in a BEFORE UPDATE trigger which will allow you to keep the query as is.

like image 21
Quassnoi Avatar answered Oct 08 '22 03:10

Quassnoi