Inserting a record with the same primary key if a primary key is defined in the table when using the SQL statement to insert a data table inserts an error:
Error code:1062. Duplicate entry ' XXXXX ' for key ' PRIMARY ' (primary key conflict)
So we have to check that the primary key is not present, and if it exists, it will be updated if it does not exist. or write SQL Process Control statements (If...else ...)
MySQL provides the same functionality as replace into and insert into. The difference is:
If you find that this row of data is already in the table ( judging by a primary key or a unique index ), the row data is first deleted and then the new data is inserted. Otherwise, insert the new data directly.
Note : Because you want to determine whether there is duplicate data based on a primary key or a unique index, the table of operations must have a primary key or a unique index. Otherwise,replace into will insert the data directly.
There are three ways to use Mysql replace into, and one of the simplest uses is to simply replace the "insert" keyword with "replace".
INSERT into T (...) the values (...) in the
Revision changed to
Replace into t (...) VALUES (...) for the ...
MySQL replace into (enhanced version of INSERT INTO)