当前位置:网站首页>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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- binom二项分布,
- BUG destroyer!!Practical debugging skills are super comprehensive
- The input input box displays the precision of two decimal places
- WPF WPF 】 【 the depth resolution of the template
- mysql基础知识(二)
- 强化学习:从入门到入坑再到拉屎
- ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)
- qlib架构
- Basic knowledge of mysql (2)
- 【ORACLE Explain 详解】
猜你喜欢
30 Years of Open Source Community | 2022 Open Atom Global Open Source Summit 30 Years of Open Source Community Special Event Held Successfully
Fusion Cloud Native, Empowering New Milestones | 2022 Open Atom Global Open Source Summit Cloud Native Sub-Forum Successfully Held
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution
ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
【云原生】DevOps(五):集成Harbor
Create componentized development based on ILRuntime hot update
Mysql应用安装后找不到my.ini文件
已解决(最新版selenium框架元素定位报错)NameError: name ‘By‘ is not defined
产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
强化学习:从入门到入坑再到拉屎
随机推荐
Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions
Unity框架设计系列:Unity 如何设计网络框架
three.js 制作3D相册
打造基于ILRuntime热更新的组件化开发
MySQL模糊查询可以使用INSTR替代LIKE
【C语言】操作符详解
BUG destroyer!!Practical debugging skills are super comprehensive
Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays
prompt.ml/15中<svg>标签使用解释
聚变云原生,赋能新里程 | 2022开放原子全球开源峰会云原生分论坛圆满召开
On Governance and Innovation | 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum was successfully held
MySQL database backup
ABC D - Distinct Trio(k元组的个数
Open Source Smart Future | 2022 OpenAtom Global Open Source Summit OpenAtom openEuler sub-forum was successfully held
input输入框展示两位小数之precision
参考代码系列_1.各种语言的Hello World
Recursive implementation of the Tower of Hanoi problem
30 Years of Open Source Community | 2022 Open Atom Global Open Source Summit 30 Years of Open Source Community Special Event Held Successfully
产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
Puzzle Game Level Design: Reverse Method--Explaining Puzzle Game Level Design