当前位置:网站首页>mysql使用on duplicate key update批量更新数据
mysql使用on duplicate key update批量更新数据
2022-07-31 04:47:00 【jiey0407】
创建测试—book科目表
CREATE TABLE `book` (
`id` int NOT NULL AUTO_INCREMENT,
`unique_code` varchar(30) NOT NULL,
`book_name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_code` (`unique_code`) USING BTREE COMMENT 'book unique_code'
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
前提需要表中存在索引:如下 unique_code 即为索引

需求:需要导入数据到book表。 当book表中存在科目的数据时,update数据值,否则insert插入一条新记录。
以往做法:循环select表中的booke记录是否存在,存在则使用update;不存在则使用insert。
做法弊端:每处理一条记录需要操作两次数据库(select、update/insert)
优化做法:使用insert语句搭配 on duplicate key update使用。
做法注意:比如上面的需求,需要用到 book表****唯一unique_code索引
一、执行如下命令
insert into book(
unique_code,
book_name
) values (
'zs-001',
'中文'
) on duplicate key update book_name='数学';

二、再次执行步骤一命令你会发现book_name值改变

简单明白,通俗易懂~~
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 数字经济时代的开源数据库创新 | 2022开放原子全球开源峰会数据库分论坛圆满召开
- 益智类游戏关卡设计:逆推法--巧解益智类游戏关卡设计
- Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays
- qlib架构
- 三道leetcode上的oj题
- Open Source Smart Future | 2022 OpenAtom Global Open Source Summit OpenAtom openEuler sub-forum was successfully held
- [Linear Neural Network] softmax regression
- ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
- 开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
- 剑指offer专项突击版第15天
猜你喜欢

The input input box displays the precision of two decimal places

ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your

MySQL基础操作

idea工程明明有依赖但是文件就是显示没有,Cannot resolve symbol ‘XXX‘

Create componentized development based on ILRuntime hot update

手把手实现图片预览插件(三)

MySQL数据库安装配置保姆级教程(以8.0.29为例)有手就行

VScode+ESP32 quickly install ESP-IDF plugin

递归实现汉诺塔问题

MySQL database backup
随机推荐
高等数学---第九章二重积分
ERP生产作业控制 金蝶
Unity手机游戏性能优化系列:针对CPU端的性能调优
Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions
BUG消灭者!!实用调试技巧超全整理
Exsl file preview, word file preview web page method
three.js make 3D photo album
STM32HAL library modifies Hal_Delay to us-level delay
MySQL 8.0.30 GA
SOLVED: After accidentally uninstalling pip (two ways to manually install pip)
Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
Fusion Cloud Native, Empowering New Milestones | 2022 Open Atom Global Open Source Summit Cloud Native Sub-Forum Successfully Held
【线性神经网络】softmax回归
Go language study notes - dealing with timeout problems - Context usage | Go language from scratch
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution
qlib架构
行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
MATLAB/Simulink & & STM32CubeMX tool chain completes model-based design development (MBD) (three)
Heavyweight | The Open Atomic School Source Line activity was officially launched
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements