Three commonly used statements for inserting data in MySQL:
Insert into indicates the data is inserted, the database will check the primary key (PrimaryKey), if there is a repeat error;
Replace into represents inserting replacement data, PrimaryKey in the requirements table, or a unique index, if the database already has data, replace with new data, and insert into if no data effects are present;
The Replace statement returns a number that indicates the number of rows affected. The number is the number of rows that are deleted and inserted. If the number is 1 for a single-line replace, the row is inserted and no rows are deleted. If the number is greater than 1, one or more old rows are deleted before the new row is inserted. If the table contains more than one unique index, and the new row replicates the values of the different old rows in different unique indexes, it is possible that a single row has replaced multiple old rows.
Insert Ignore indicates that if the same record already exists, the current new data is ignored;
The following are the differences between code descriptions, as follows:
CREATE TABLE TESTTB (
ID int NOT NULL PRIMARY key,
Name varchar (50),
Age int
);
Insert into TESTTB (id,name,age) VALUES (1, "BB", 13);
SELECT * from TESTTB;
Insert Ignore into TESTTB (Id,name,age) VALUES (1, "AA", 13);
SELECT * from testtb;//is still 1, "BB", 13, because ID is primary key, duplicate primary key but use ignore error is ignored
Replace into TESTTB (id,name,age) VALUES (1, "AA", 12);
SELECT * from TESTTB; Data becomes 1, "AA", 12
The difference between insert into and replace into and insert ignore in MySQL