IGNORE关键字
作用:
如果在向数据库中增加一条与主键重复的数据,正常情况是会报错的。但是使用IGNORE后,会发出一条警告,数据也不会插入。如果没有重复,就正常插入。
使用方法:
- 建表语句
DROP TABLE IF EXISTS `employees` ;
CREATE TABLE `employees` (
`emp_no` INT(11) NOT NULL,
`birth_date` DATE NOT NULL,
`first_name` VARCHAR(14) NOT NULL,
`last_name` VARCHAR(16) NOT NULL,
`gender` CHAR(1) NOT NULL,
`hire_date` DATE NOT NULL,
PRIMARY KEY (`emp_no`));
- 插入数据
INSERT INTO employees VALUES(10001,'1953-09-02','Georgi','Facello','M','1986-06-26');
INSERT INTO employees VALUES(10002,'1964-06-02','Bezalel','Simmel','F','1985-11-21');
INSERT INTO employees VALUES(10003,'1959-12-03','Parto','Bamford','M','1986-08-28');
INSERT INTO employees VALUES(10004,'1954-05-01','Chirstian','Koblick','M','1986-12-01');
- 再次插入数据的时候
INSERT INTO employees VALUES(10001,'1953-09-02','Georgi','Facello','M','1986-06-26');
执行失败
1 queries executed, 0 success, 1 errors, 0 warnings
查询:INSERT INTO employees VALUES(10001,'1953-09-02','Georgi','Facello','M','1986-06-26')
错误代码: 1062
Duplicate entry '10001' for key 'PRIMARY'
执行耗时 : 0 sec
传送时间 : 0 sec
总耗时 : 0.007 sec
INSERT IGNORE INTO employees VALUES(10001,'1953-09-02','Georgi','Facello','M','1986-06-26');
执行成功
1 queries executed, 1 success, 0 errors, 0 warnings
查询:INSERT IGNORE INTO employees VALUES(10001,'1953-09-02','Georgi','Facello','M','1986-06-26')
共 0 行受到影响
执行耗时 : 0 sec
传送时间 : 0.002 sec
总耗时 : 0.002 sec
查询结果也没有多一样
参考: mySql的ignore SQL35 批量插入数据,不使用replace操作
|