当前位置:网站首页>The difference between insert ignore and insert into

The difference between insert ignore and insert into

2022-06-26 07:07:00 Talk to me

INSERT IGNORE And INSERT INTO The difference is that INSERT IGNORE Will ignore the database already exists The data of , If there is no data in the database , Just insert new data , If you have data, skip this data . In this way, you can keep the existing data in the database , To insert data in the gap .

eg: insert ignore into table(name)  select  name from table2 

mysql There are three kinds of statements to insert data :
insert into Means insert data , The database checks the primary key (PrimaryKey), If there is a repetition, an error will be reported ;
replace into Indicates insert replacement data , What is in the demand table PrimaryKey, perhaps unique Index words , If the database already has data , Replace with new data , If there's no data effect, it's the same as insert into equally ;
REPLACE Statement will return a number , To indicate the number of rows affected . This number is the sum of the number of rows deleted and inserted . If for a single line REPLACE The number is 1, Then a line is inserted , At the same time, no lines have been deleted . If the number is greater than 1, Before the new line is inserted , One or more old lines have been deleted . If the table contains multiple unique indexes , And the new row copies the values of different old rows in different unique indexes , It is possible that a single line replaces multiple old lines .
 
insert ignore Express , If the same record already exists in , Ignore the current new data ;

The difference is explained by the code below , 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 It's the primary key , Duplicate primary key but used ignore The error is ignored 
replace into testtb(id,name,age)values(1,"aa",12);
select * from testtb; // The data becomes 1,"aa",12

 

原网站

版权声明
本文为[Talk to me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260702004367.html