Using INSERT IGNORE with MySQL to prevent duplicate key errors

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: An error will occur when inserting a new record in MySQL if the primary key specified in the insert query already exists.

An error will occur when inserting a new record in MySQL if the primary key specified in the insert query already exists. Using the "IGNORE" keyword prevents errors from occuring and other queries are still able to be run.

Why?

Although you shouldn't normally attempt to insert a record without first checking if the primary key you want to use has already been used, there may be times when this is required, such as when multiple developers need to update their own copies of a database, and a particular record may already exist in one or other of the databases.

Inserting a single record

The syntax is simple - just add "IGNORE" after "INSERT" like so:

INSERT IGNORE INTO mytable
    (primaryKey, field1, field2)
VALUES
    ('abc', 1, 2);

Inserting multiple records

When inserting mutiple records at once, any that cannot be inserting will not be, but any that can will be:

INSERT IGNORE INTO mytable
    (primaryKey, field1, field2)
VALUES
    ('abc', 1, 2),
    ('def', 3, 4),
    ('ghi', 5, 6);

In the second example above, if records already existed for 'abc' and 'def' then those two records would not be inserted, but the record for 'ghi' would be.

https://www.electrictoolbox.com/mysql-insert-ignore/

 

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
8月前
|
SQL 关系型数据库 MySQL
MySQL唯一约束(UNIQUE KEY)
MySQL唯一约束(UNIQUE KEY)
229 0
|
6月前
|
XML 关系型数据库 MySQL
【MySQL异常】ExecutorException: Error getting generated key or setting result to parameter object
【MySQL异常】ExecutorException: Error getting generated key or setting result to parameter object
122 0
|
21天前
|
关系型数据库 MySQL
MySQL创建表出现 Specified key was too long; max key length is 767 bytes
MySQL创建表出现 Specified key was too long; max key length is 767 bytes
18 2
|
5月前
|
关系型数据库 MySQL 数据库
连接MySQL时报错:Public Key Retrieval is not allowed的解决方法
连接MySQL时报错:Public Key Retrieval is not allowed的解决方法
165 1
|
7月前
|
关系型数据库 MySQL
Mysql 主键冲突(ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY')
Mysql 主键冲突(ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY')
316 0
|
4月前
|
关系型数据库 MySQL
零基础带你学习MySQL—foreign key 外键(二十六)
零基础带你学习MySQL—foreign key 外键(二十六)
|
4月前
|
关系型数据库 MySQL
零基础带你学习MySQL—primary key主键(二十三)
零基础带你学习MySQL—primary key主键(二十三)
|
5月前
|
SQL 关系型数据库 MySQL
Mysql统计技巧:ON DUPLICATE KEY UPDATE用法
Mysql统计技巧:ON DUPLICATE KEY UPDATE用法
|
5月前
|
关系型数据库 MySQL 数据库
Mysql中key与index区别
Mysql中key与index区别
|
6月前
|
关系型数据库 MySQL Java
【MySQL异常解决】Caused by: com.mysql.cj.exceptions.UnableToConnectException: Public Key Retrieval is not
【MySQL异常解决】Caused by: com.mysql.cj.exceptions.UnableToConnectException: Public Key Retrieval is not
88 0