温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL 5.5 INSERT ... ON DUPLICATE KEY UPDATE语句说明

发布时间:2020-08-08 15:06:38 来源:ITPUB博客 阅读:159 作者:feelpurple 栏目:MySQL数据库
执行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)
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI