mysql insert into, insert ignore into, insert into on duplicate key update
Posted on 2021-02-28 23:11 翔云123456 阅读(112) 评论(0) 编辑 收藏 举报目录
几个sql的执行情况汇总如下表:
指令 | 已存在 | 不存在 |
---|---|---|
insert | 报错 | 插入 |
insert ignore | 忽略 | 插入 |
insert into on duplicate key update | 更新 | 插入 |
insert ignore into on duplicate key update | 更新 | 插入 |
数据表要求有主键或唯一键。
另外,以上操作,都会使自增字段自增。
下面做下具体的测试。
建表
CREATE TABLE `test5` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`age` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `name_UNIQUE` (`name`)
) ENGINE=InnoDB;
id
是主键,自增,name
是唯一键。
insert into
> insert into test5 (name, age) values('John', 1);
Query OK, 1 row affected (0.00 sec)
查询
mysql> select * from test5;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | John | 1 |
+----+------+-----+
1 row in set (0.00 sec)
已插入新记录。
再次插入
mysql> insert into test5 (name, age) values('John', 1);
ERROR 1062 (23000): Duplicate entry 'John' for key 'name_UNIQUE'
报错,唯一键冲突。
insert ignore into
mysql> insert ignore into test5 (name, age) values('John', 1);
Query OK, 0 rows affected, 1 warning (0.00 sec)
查询
mysql> select * from test5;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | John | 1 |
+----+------+-----+
1 row in set (0.00 sec)
ignore 会忽略错误。
insert into on duplicate update
mysql> insert into test5 (name, age) values('John', 2) on duplicate key update age=2;
Query OK, 2 rows affected (0.00 sec)
查询
mysql> select * from test5;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | John | 2 |
+----+------+-----+
1 row in set (0.00 sec)
age更新为2。
不存在记录时
mysql> insert into test5 (name, age) values('Alex', 2) on duplicate key update age=2;
Query OK, 1 row affected (0.00 sec)
查询
mysql> select * from test5;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | John | 2 |
| 7 | Alex | 2 |
+----+------+-----+
2 rows in set (0.00 sec)
会插入新记录。
insert ignore into on duplicate update
mysql> insert ignore into test5 (name, age) values('John', 3) on duplicate key update age=3;
Query OK, 2 rows affected (0.01 sec)
查询
mysql> select * from test5;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | John | 3 |
| 7 | Alex | 2 |
+----+------+-----+
2 row in set (0.00 sec)
John的age更新为3。
不存在记录时
mysql> insert ignore into test5 (name, age) values('Bob', 3) on duplicate key update age=3;
Query OK, 1 row affected (0.00 sec)
mysql> select * from test5;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | John | 3 |
| 7 | Alex | 2 |
| 8 | Bob | 3 |
+----+------+-----+
3 rows in set (0.00 sec)
会插入新记录。
Just try, don't shy.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现