While executing an INSERT
statement with many rows, I want to skip duplicate entries that would otherwise cause failure. After some research, my options appear
If you want to insert in the table and on the conflict of the primary key or unique index it will update the conflicting row instead of inserting that row.
Syntax:
insert into table1 set column1 = a, column2 = b on duplicate update column2 = c;
Now here, this insert statement may look different what you have seen earlier. This insert statement trying to insert a row in table1 with the value of a and b into column column1 and column2 respectively.
Let's understand this statement in depth:
For example: here column1 is defined as the primary key in table1.
Now if in table1 there is no row having the value “a” in column1. So this statement will insert a row in the table1.
Now if in table1 there is a row having the value “a” in column2. So this statement will update the row’s column2 value with “c” where the column1 value is “a”.
So if you want to insert a new row otherwise update that row on the conflict of the primary key or unique index.
Read more on this link