Mysql 主键冲突(ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY')

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: Mysql 主键冲突(ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY')

  • 在插入数据的时候,如果插入的数据主键已经存在,那么这条数据就会报错主键冲突,并终止执行:


ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'


  • 解决办法:


1、忽略:ignore 忽略错误,正常终止插入,数据不会改变。


insert ignore into 表名 (字段列表) values (值列表);


2、替换:replace 将新数据完整覆盖旧数据。


replace into 表名 (字段列表) values (值列表);


3、更新:on duplicate key 可以预先设定需要覆盖的旧数据,如果发生冲突,则更新指定的字段为指定的新值即可。


insert into 表名 (字段列表) values (值列表) on duplicate key update 字段名=新值, 字段名=新值, ...;


  • 测试数据:


mysql> select * from test; +----+-------+------+------+ | id | name | sex | age | +----+-------+------+------+ | 1 | name1 | 男 | 5 | | 2 | name2 | 女 | 10 | | 3 | name3 | 男 | 15 | | 4 | name4 | 男 | 20 | +----+-------+------+------+


``` 插入一条已经存在的主键数据 id = 1 mysql> insert into test (id, name, sex, age) values (1, 'name5', '女', 25); ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'


忽略 ignore 冲突数据不变 mysql> insert ignore into test (id, name, sex, age) values (1, 'name5', '女', 25); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> select * from test; +----+-------+------+------+ | id | name | sex | age | +----+-------+------+------+ | 1 | name1 | 男 | 5 | | 2 | name2 | 女 | 10 | | 3 | name3 | 男 | 15 | | 4 | name4 | 男 | 20 | +----+-------+------+------+


替换 replace 直接替换冲突主键的值为最新的数据 mysql> replace into test (id, name, sex, age) values (1, 'name5', '女', 25); mysql> select * from test; +----+-------+------+------+ | id | name | sex | age | +----+-------+------+------+ | 1 | name5 | 女 | 25 | | 2 | name2 | 女 | 10 | | 3 | name3 | 男 | 15 | | 4 | name4 | 男 | 20 | +----+-------+------+------+


更新 on duplicate key 如果发生冲突,则更新指定的字段为指定的新值即可 mysql> insert into test (id, name, sex, age) values (1, 'name1', '男', 5) on duplicate key update name='name1', sex=' 男', age=5; mysql> select * from test; +----+-------+------+------+ | id | name | sex | age | +----+-------+------+------+ | 1 | name1 | 男 | 5 | | 2 | name2 | 女 | 10 | | 3 | name3 | 男 | 15 | | 4 | name4 | 男 | 20 | +----+-------+------+------+ ```


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3月前
|
NoSQL 关系型数据库 MySQL
MySQL主键与索引
MySQL主键与索引
58 1
|
21天前
|
关系型数据库 MySQL
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file
24 0
|
27天前
|
SQL 关系型数据库 MySQL
SQL Error (2013): Lost connection to MySQL server at 'waiting for initial communication packet', sys...
SQL Error (2013): Lost connection to MySQL server at 'waiting for initial communication packet', sys...
|
3月前
|
SQL 数据采集 关系型数据库
如何解决MySQL报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQL?
如何解决MySQL报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQL?
|
29天前
|
关系型数据库 MySQL 数据安全/隐私保护
MySQL连接ERROR 2059 (HY000): Authentication plugin ‘caching_sha2_password‘ cannot be loaded
MySQL连接ERROR 2059 (HY000): Authentication plugin ‘caching_sha2_password‘ cannot be loaded
28 0
|
1月前
|
关系型数据库 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
20 2
|
1月前
|
缓存 关系型数据库 MySQL
为啥MySQL官方不推荐使用uuid或者雪花id作为主键
为啥MySQL官方不推荐使用uuid或者雪花id作为主键
25 1
|
2月前
|
存储 关系型数据库 MySQL
用雪花 ID 和 UUID 做 MySQL 主键,可以吗?
用雪花 ID 和 UUID 做 MySQL 主键,可以吗?
35 0
用雪花 ID 和 UUID 做 MySQL 主键,可以吗?
|
7天前
|
SQL 存储 关系型数据库
MySQL Cluster集群安装及使用
MySQL Cluster集群安装及使用
|
11天前
|
关系型数据库 MySQL 数据库
《MySQL 简易速速上手小册》第1章:MySQL 基础和安装(2024 最新版)
《MySQL 简易速速上手小册》第1章:MySQL 基础和安装(2024 最新版)
36 4