MySQL Replace into usage (insert into enhanced version) is often encountered when inserting data into a table: 1. First, determine whether the data exists; 2. If it does not exist, then insert; 3. If present, update. This can be handled in SQL Server: if not EXISTS (select 1 from t where id = 1) inserts into T (ID, Update_time) VALUES (1, Getdat E ()) else update t Set update_time = GETDATE () where id = 1 So how do you implement such logic in MySQL? Don't worry! There is an easier way to do this in MySQL: replace Intoreplace into t (ID, Update_time) VALUES (1, now ()), or replace into t (ID, update_time) Select 1, no W (); Replace into is similar to insert, except that replace into first attempts to insert data into the table, 1. 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. 2. Otherwise, insert the new data directly. Note that the table in which the data is inserted must have a primary key or a unique index! Otherwise, replace into will insert the data directly, which will result in duplicate data appearing in the table. MySQL replace into has three different forms: 1. Replace into Tbl_name (col_name, ...) VALUES (...) 2. Replace into Tbl_name (col_name, ...) Select ... 3. Replace into tbl_name set Col_name=value, ... The first two forms are used more. The "into" keyword can be omitted, but it is better to add "into" so that the meaning is more intuitive. In addition, for columns that are not given a value, MySQL automatically assigns default values to those columns.