MySQL 5.5 INSERT ... ON DUPLICATE KEY UPDATE语句说明
发表于:2025-01-19 作者:千家信息网编辑
千家信息网最后更新 2025年01月19日,执行INSERT ... ON DUPLICATE KEY UPDATE语句,如果INSERT的语句插入的值和已有的UNIQUE索引或主键重复的话,MySQL会更新已存在的行。测试没有主键和UNIQU
千家信息网最后更新 2025年01月19日MySQL 5.5 INSERT ... ON DUPLICATE KEY UPDATE语句说明
mysql> select * from dept2;
+--------+-------------+-------------+
| deptno | dname | report_date |
+--------+-------------+-------------+
| 10 | Research | 2016-06-03 |
| 20 | Maintenance | 2016-06-03 |
| 30 | Leader | 2016-06-03 |
| 40 | Market | 2015-08-02 |
+--------+-------------+-------------+
4 rows in set (0.00 sec)
mysql> desc dept2;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| deptno | int(5) | NO | MUL | NULL | |
| dname | varchar(14) | YES | | NULL | |
| report_date | date | YES | MUL | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> INSERT INTO dept2 (deptno,dname,report_date) VALUES (20,'Development','2010-10-30')
-> ON DUPLICATE KEY UPDATE report_date='2010-10-30';
Query OK, 1 row affected (0.01 sec)
mysql> select * from dept2;
+--------+-------------+-------------+
| deptno | dname | report_date |
+--------+-------------+-------------+
| 10 | Research | 2016-06-03 |
| 20 | Maintenance | 2016-06-03 |
| 30 | Leader | 2016-06-03 |
| 40 | Market | 2015-08-02 |
| 20 | Development | 2010-10-30 |
+--------+-------------+-------------+
5 rows in set (0.00 sec)
mysql> delete from dept2 where deptno=20 and report_date=date'2010-10-30';
Query OK, 1 row affected (0.01 sec)
mysql> select * from dept2;
+--------+-------------+-------------+
| deptno | dname | report_date |
+--------+-------------+-------------+
| 10 | Research | 2016-06-03 |
| 20 | Maintenance | 2016-06-03 |
| 30 | Leader | 2016-06-03 |
| 40 | Market | 2015-08-02 |
+--------+-------------+-------------+
4 rows in set (0.00 sec)
增加主键,再进行测试
mysql> alter table dept2 add primary key(deptno);
Query OK, 0 rows affected (0.28 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> INSERT INTO dept2 (deptno,dname,report_date) VALUES (20,'Development','2010-10-30')
-> ON DUPLICATE KEY UPDATE report_date='2010-10-30';
Query OK, 2 rows affected (0.14 sec)
mysql> select * from dept2;
+--------+-------------+-------------+
| deptno | dname | report_date |
+--------+-------------+-------------+
| 10 | Research | 2016-06-03 |
| 20 | Maintenance | 2010-10-30 |
| 30 | Leader | 2016-06-03 |
| 40 | Market | 2015-08-02 |
+--------+-------------+-------------+
4 rows in set (0.00 sec)
执行INSERT ... ON DUPLICATE KEY UPDATE语句,如果INSERT的语句插入的值和已有的UNIQUE索引或主键重复的话,MySQL会更新已存在的行。
测试没有主键和UNIQUE索引的表mysql> select * from dept2;
+--------+-------------+-------------+
| deptno | dname | report_date |
+--------+-------------+-------------+
| 10 | Research | 2016-06-03 |
| 20 | Maintenance | 2016-06-03 |
| 30 | Leader | 2016-06-03 |
| 40 | Market | 2015-08-02 |
+--------+-------------+-------------+
4 rows in set (0.00 sec)
mysql> desc dept2;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| deptno | int(5) | NO | MUL | NULL | |
| dname | varchar(14) | YES | | NULL | |
| report_date | date | YES | MUL | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> INSERT INTO dept2 (deptno,dname,report_date) VALUES (20,'Development','2010-10-30')
-> ON DUPLICATE KEY UPDATE report_date='2010-10-30';
Query OK, 1 row affected (0.01 sec)
mysql> select * from dept2;
+--------+-------------+-------------+
| deptno | dname | report_date |
+--------+-------------+-------------+
| 10 | Research | 2016-06-03 |
| 20 | Maintenance | 2016-06-03 |
| 30 | Leader | 2016-06-03 |
| 40 | Market | 2015-08-02 |
| 20 | Development | 2010-10-30 |
+--------+-------------+-------------+
5 rows in set (0.00 sec)
mysql> delete from dept2 where deptno=20 and report_date=date'2010-10-30';
Query OK, 1 row affected (0.01 sec)
mysql> select * from dept2;
+--------+-------------+-------------+
| deptno | dname | report_date |
+--------+-------------+-------------+
| 10 | Research | 2016-06-03 |
| 20 | Maintenance | 2016-06-03 |
| 30 | Leader | 2016-06-03 |
| 40 | Market | 2015-08-02 |
+--------+-------------+-------------+
4 rows in set (0.00 sec)
增加主键,再进行测试
mysql> alter table dept2 add primary key(deptno);
Query OK, 0 rows affected (0.28 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> INSERT INTO dept2 (deptno,dname,report_date) VALUES (20,'Development','2010-10-30')
-> ON DUPLICATE KEY UPDATE report_date='2010-10-30';
Query OK, 2 rows affected (0.14 sec)
mysql> select * from dept2;
+--------+-------------+-------------+
| deptno | dname | report_date |
+--------+-------------+-------------+
| 10 | Research | 2016-06-03 |
| 20 | Maintenance | 2010-10-30 |
| 30 | Leader | 2016-06-03 |
| 40 | Market | 2015-08-02 |
+--------+-------------+-------------+
4 rows in set (0.00 sec)
语句
索引
测试
更新
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
戴尔服务器 r710
数据库集合查询和统计查询
计算机网络安全入侵
网络安全管理职位 公务员
数据库表空间存放位置
软件开发 劳务费 税率
上海市网络安全信息中心
医学影像病例数据库
无线传感器网络技术有什么优势
数字媒体数据库技术ppt
联通云盾网络安全管理平台认证
关于车联网的网络安全论文
海康服务器远程重装系统
赛维益软件开发
医院网络安全检查工作方案
网络技术开发 技术转让
怎么控制软件开发进度
网络安全 人民政府
欧盟医疗器械数据库
emule没有服务器
单招软件开发月薪多少
服务器cpu核心
金融行业网络安全保障
唐县健康信息平台数据库
石河子天气预报软件开发
联通云盾网络安全管理平台认证
数据库过程
网络安全红蓝对抗
上海紫山互联网科技有限公司
金仓数据库绿色版