MySQL 5.5 INSERT ... ON DUPLICATE KEY UPDATE語句說明
執行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)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2113722/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql INSERT ... ON DUPLICATE KEY UPDATEMySql
- MySQL insert on duplicate key update 死鎖MySql
- MySQL批量Insert應用ON DUPLICATE KEY UPDATEMySql
- MySQL 關於 INSERT INTO...ON DUPLICATE KEY UPDATE 的使用MySql
- MySQL 5.7 SELECT ... LOCK IN SHARE MODE|FOR UPDATE語句說明MySql
- MySQL_插入更新 ON DUPLICATE KEY UPDATEMySql
- BUG: pymysql executemany不支援insert on duplicate key updateMySql
- mysql實現merge功能之DUPLICATE key UPDATE語法MySql
- MySQL 5.7 EXPLAIN EXTENDED語句說明MySqlAI
- 翻譯:insert on duplicate key update(已提交到MariaDB官方手冊)
- MySQL 的CASE WHEN 語句使用說明MySql
- mysql語句分析工具explain使用說明MySqlAI
- MySQL 5.5 mysqldump備份說明MySql
- SQLite語句(二):INSERT DELETE UPDATE SELECTSQLitedelete
- LINQ to SQL語句之Insert/Update/Delete操作SQLdelete
- DBeaver如何生成select,update,delete,insert語句delete
- REPLACE與INSERT INTO ... ON DUPLICATE KEY總結
- mysql update join,insert select 語法MySql
- MySQL -update語句流程總結MySql
- MySQL的update語句避坑MySql
- MySql與Sql Server Update語句MySqlServer
- KunlunDB功能之insert/update/delete...returning語句delete
- 輕量ORM-SqlRepoEx (四)INSERT、UPDATE、DELETE 語句ORMSQLdelete
- MySQL INSERT IGNORE語句的使用MySql
- MySQL 5.5編譯安裝cmake引數說明MySql編譯
- MySQL 5.5 SHOW PROFILE、SHOW PROFILES語句介紹MySql
- SQLite Insert 語句SQLite
- mysql 語句不能update的解決辦法MySql
- mysql insert語句錯誤問題解決MySql
- MySQL 5.5 LOCK TABLES 和 UNLOCK TABLES 語句介紹MySql
- MySQL 5.5 FLUSH TABLES WITH READ LOCK語句介紹MySql
- mysql操作命令梳理(5)-執行sql語句查詢即mysql狀態說明MySql
- mysql 5.5 中自連線update and deleteMySqldelete
- Mysql跨表更新 多表update sql語句總結MySql
- MySQL 5.7 複製控制語句SET GLOBAL sql_slave_skip_counter說明MySql
- MySQL:一個簡單insert語句的大概流程MySql
- 【Mysql】兩條insert 語句產生的死鎖MySql
- Q:[Vue warn]: Duplicate keys detected: ‘PAYACT‘. This may cause an update error.VueError