The difference between REPLACE INTO and INSERT INTO in mysql

The difference between REPLACE INTO and INSERT INTO in mysql

 

        The functions of REPLACE INTO and INSERT INTO are similar, both are like inserting data into a table, the difference is: REPLACE INTO first tries to insert data into the table:

  • If it is found that there is already this row of data in the table (judged by the primary key or unique index), delete this row of data first, and then insert new data.
  • Otherwise, insert new data directly.

         To be careful of:

  • The table into which data is inserted must have a primary key or a unique index! Otherwise, REPLACE INTO will insert data directly, which will result in duplicate data in the table.
  • Unless the table has a PRIMARY KEY or UNIQUE index, there is no point in using a REPLACE statement.
  • To be able to use REPLACE, you must have both INSERT and DELETE permissions on the table.
  • The REPLACE statement returns a number that indicates the number of rows affected. The number is the sum of the number of rows deleted and inserted.

         If the primary key has been set in the table, then insert a piece of data that already exists in the table into the table, then delete this row of data first, and then insert new data, the affected rows: 2; if insert a non-existent primary key into the table data, then insert new data directly, affected rows: 1.

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326941450&siteId=291194637