INSERT IGNORE
INSERT IGNORE与INSERT INTO的区别
就是INSERT IGNORE会忽略数据库中已经存在 的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据。这样就可以保留数据库中已经存在数据,达到在间隙中插入数据的目的。
mysql> create table testtb( -> id int not null primary key, -> name varchar(50), -> age int -> ); Query OK, 0 rows affected (0.01 sec) mysql> mysql> select * from testtb; Empty set (0.01 sec) mysql> insert into testtb(id,name,age)values(1,"bb",13); Query OK, 1 row affected (0.00 sec) mysql> insert ignore into testtb(id,name,age)values(1,"aa",13); Query OK, 0 rows affected, 1 warning (0.01 sec) ---因为 ignore 插入,虽然没有报错,但是因为主键没插入成功。 mysql> show warnings; +---------+------+---------------------------------------+ | Level | Code | Message | +---------+------+---------------------------------------+ | Warning | 1062 | Duplicate entry '1' for key 'PRIMARY' | +---------+------+---------------------------------------+ 1 row in set (0.00 sec) mysql> mysql> mysql> mysql> select * from testtb; +----+------+------+ | id | name | age | +----+------+------+ | 1 | bb | 13 | +----+------+------+ 1 row in set (0.00 sec)
mysql> insert ignore into testtb(id,name,age)values(2,"bb",13); --主键不冲突,插入成功。
Query OK, 1 row affected (0.00 sec)
mysql> select * from testtb;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | bb | 13 |
| 2 | bb | 13 |
+----+------+------+
2 rows in set (0.00 sec)
monkeybron