I have a table having 3 columns like this
CREATE TABLE my_table (
A_id INTEGER NOT NULL,
B_id INTEGER NOT NULL,
C CHARACTER VARYING(50) NOT NULL,
PRIMARY KEY (A_id, B_id, C)
);
I have to update some values in column B_id with other values. for which I tried this query
UPDATE my_table
SET B_id = 10
WHERE B_id = 20
But the problem is that this gives me a "duplicate key value violates unique constraint"
since at some places table has data like this
A_id, B_id, C
1, 10, a
1, 20, a ## this row seems to cause constraint problem
1, 20, b
2, 20, a
2, 20, b
I want the above data to become look like this
A_id, B_id, C
1, 10, a
1, 10, b
2, 10, a
2, 10, b
so in general if the key combination is already present then delete the one's with value 20 otherwise update the value. Thanks in anticipation !