溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL 5.5 INSERT ... ON DUPLICATE KEY UPDATE語句說明

發(fā)布時間:2020-08-08 15:06:38 來源:ITPUB博客 閱讀:159 作者:feelpurple 欄目:MySQL數(shù)據(jù)庫
執(zhí)行INSERT ... ON DUPLICATE KEY UPDATE語句,如果INSERT的語句插入的值和已有的UNIQUE索引或主鍵重復(fù)的話,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)

增加主鍵,再進(jìn)行測試

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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI