MySQL中INSERT IGNORE INTO和REPLACE INTO的使用

INSERT IGNORE 与INSERT INTO的区别就是INSERT IGNORE会忽略数据库中已经存在 的数据,若是数据库没有数据,就插入新的数据,若是有数据的话就跳过这条数据。这样就能够保留数据库中已经存在数据,达到在间隙中插入数据的目的。
eg:    insert ignore into table(name)  select  name from table2 mysql

mysql中insert into和replace into以及insert ignore用法区别: 
mysql中经常使用的三种插入数据的语句: 
insert into表示插入数据,数据库会检查主键,若是出现重复会报错; 
replace into表示插入替换数据,需求表中有PrimaryKey,或者unique索引,若是数据库已经存在数据,则用新数据替换,若是没有数据效果则和insert into同样; 
insert ignore表示,若是中已经存在相同的记录,则忽略当前新数据; 
下面经过代码说明之间的区别,以下: 
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;//还是1,“bb”,13,由于id是主键,出现主键重复但使用了ignore则错误被忽略 
replace into testtb(id,name,age)values(1,"aa",12); 
select * from testtb; //数据变为1,"aa",12sql

相关文章
相关标签/搜索