I want to update multiple rows identified by unique key without inserting new rows.
Below is my table:
CREATE TABLE `insert_update_ignore` (
`obj_id` int(11) NOT NULL,
`obj_type` tinyint(4) NOT NULL,
`value` int(11) DEFAULT '-1',
UNIQUE KEY `unique_key` (`obj_id`,`obj_type`))
ENGINE=InnoDB DEFAULT CHARSET=utf8
The table has some existing records:
mysql> select * from insert_update_ignore;
+--------+----------+-------+
| obj_id | obj_type | value |
+--------+----------+-------+
| 1 | 1 | -1 |
| 1 | 2 | -1 |
| 2 | 1 | -1 |
| 2 | 2 | -1 |
+--------+----------+-------+
I have some values to update the value
column in the table.
INSERT INTO insert_update_ignore(obj_id, obj_type, value)
VALUES(1, 1, 1),
(1, 2, 3),
(2, 1, 1),
(2, 2, 5),
(3, 1, 10)
ON DUPLICATE KEY UPDATE
value = VALUES(value);
This almost done the job except that the last value, i.e. (3, 1, 10) is inserted into the table. This one should not be inserted because the unique key (3, 1) do not exists before.
How can i just do the UPDATE
part, but not INSERT
? The table schema can be changed if
better implementation exists. The values to be update are calculated from other database(running on different machine and different port).
I dig out the one below do the job, but the statement is too big if the values are in thousands. Any more elegant ways to do this? Thanks if any one can help on this. P.S I am coding this in Python and using the MySQLdb library.
UPDATE insert_update_ignore
SET value = CASE
WHEN (obj_id = 1 AND obj_type = 1) THEN 1
WHEN (obj_id = 1 AND obj_type = 2) THEN 3
WHEN (obj_id = 2 AND obj_type = 1) THEN 1
WHEN (obj_id = 2 AND obj_type = 2) THEN 5
WHEN (obj_id = 3 AND obj_type = 1) THEN 10
ELSE value
END
WHERE (obj_id, obj_type) IN ((1, 1), (1,2), (2, 1), (2, 2), (3, 1));