Mysql插入数据的SQL语句主要有:

1、insert into表示插入数据,数据库会检查主键,如果出现重复会报错;

2、replace into表示插入替换数据,需求表中有PrimaryKey,或者unique索引,如果数据库已经存在数据,则用新数据替换,如果没有数据效果则和insert into一样;

3、insert ignore表示,如果表中如果已经存在相同的记录,则忽略当前新数据;



1. create table testtb(
2. id int not null primary key,
3. name varchar(50),
4. age int
5. );
6. insert into testtb(id,name,age)values(1,'bb',13);
7. select * from testtb;
8. insert ignore into testtb(id,name,age)values(1,'aa',13);
9. select * from testtb;
10. replace into testtb(id,name,age)values(1,"aa",12);
11. select * from testtb;

4、on duplicate key update 使用该语法可在插入记录的时候先判断记录是否存在,如果不存在则插入,否则更新,很方便,无需执行两条SQL 

注:需要设置Mysql表的unique唯一索引值