mysql一次更新多條不同的記錄
最近oschina上又有朋友問到了mysql中一次更新多條不同的記錄的方法,我知道的方法有兩種,使用on duplicate key update語法和使用 replace into語法。
這兩個語法都需要主鍵索引或唯一索引支持,下面舉例說明。
測試用的表結構和數據
1
CREATE TABLE `t` (
2
`id` int(11) NOT NULL AUTO_INCREMENT,
3
`c1` varchar(50) NOT NULL DEFAULT '',
4
`c2` varchar(50) NOT NULL DEFAULT '',
5
`c3` varchar(50) NOT NULL DEFAULT '',
6
PRIMARY KEY (`id`),
7
UNIQUE KEY `c1` (`c1`)
8
) ENGINE=InnoDB AUTO_INCREMENT=125 DEFAULT CHARSET=utf8 ;
9
insert into t values(1,2,3,4),(5,6,7,8);
on duplicate key update 語法
on duplicate key update 語法的官方說明http://docs.oracle.com/cd/E17952_01/refman-5.1-en/insert-on-duplicate.html
1
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, MySQL performs an UPDATE of the old row.
它會先執行插入操作,碰到有主鍵或唯一索引的列發生沖突時,對沖突的這一行,執行update操作,更新sql語句中指定的某幾列。如果所有的列都不沖突,此語法和簡單的insert into語法效果一樣。例如:
01
mysql> insert into t (id,c1,c2)values(1,20,30),(5,60,70) on duplicate key update c1=values(c1),c2=values(c2);
02
Query OK, 4 rows affected (0.00 sec)
03
Records: 2 Duplicates: 2 Warnings: 0
04
05
mysql> select * from t;
06
+----+----+----+----+
07
| id | c1 | c2 | c3 |
08
+----+----+----+----+
09
| 1 | 20 | 30 | 4 |
10
| 5 | 60 | 70 | 8 |
11
+----+----+----+----+
12
2 rows in set (0.00 sec)
結果是c1,c2這兩列被更新了,c3這一列沒有變。
replace into 語法
replace into 語法的官方說明http://docs.oracle.com/cd/E17952_01/refman-5.5-en/replace.html
1
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.
replace和insert所作的工作完全相同,區別是當碰到有主鍵或唯一索引的列發生沖突時,對沖突的這一行,在insert前會先對這行數據執行delete操作。效果是這一行中沒有被指定值的列會被更新成本列的默認值,如果所有的列都不沖突,此語法和簡單的insert into語法效果一樣。例如:
完全替換兩行記錄
01
mysql> replace into t (id,c1) values(1,200),(5,600);
02
Query OK, 4 rows affected (0.00 sec)
03
Records: 2 Duplicates: 2 Warnings: 0
04
05
mysql> select * from t;
06
+----+-----+----+----+
07
| id | c1 | c2 | c3 |
08
+----+-----+----+----+
09
| 1 | 200 | | |
10
| 5 | 600 | | |
11
+----+-----+----+----+
12
2 rows in set (0.00 sec)
不使用ID,使用唯一索引來替換記錄
01
mysql> replace into t (c1,c2) values(200,3),(600,7);
02
Query OK, 4 rows affected (0.00 sec)
03
Records: 2 Duplicates: 2 Warnings: 0
04
05
mysql> select * from t;
06
+-----+-----+----+----+
07
| id | c1 | c2 | c3 |
08
+-----+-----+----+----+
09
| 127 | 200 | 3 | |
10
| 128 | 600 | 7 | |
11
+-----+-----+----+----+
12
2 rows in set (0.00 sec)
效果是id也被替換掉了.
當使用唯一索引,並重且給唯一索引這一列加了重復的值時
01
mysql> replace into t (id,c1) values(127,200),(128,200);
02
Query OK, 5 rows affected (0.00 sec)
03
Records: 2 Duplicates: 3 Warnings: 0
04
05
mysql> select * from t;
06
+-----+-----+----+----+
07
| id | c1 | c2 | c3 |
08
+-----+-----+----+----+
09
| 128 | 200 | | |
10
+-----+-----+----+----+
11
1 row in set (0.00 sec)
最後為什麼只剩一條記錄了?插入(127,200)這一行前,會刪掉id=127或c1=200的行,然後執行插入。插入(128,200)這一行前,會刪掉id=128或c1=200的行,剛好前面插入的那一行中,c1=200,所以前面那一行也被刪掉了,最後只留下了一行。
一次最多能更新多少條記錄?
mysql中沒有一次更新記錄數的限制,但是有sql語句長度的限制。如果需要執行超長的sql語句,需要調整max_allowed_packet這個配置參數。
max_allowed_packet參數的官方說明http://docs.oracle.com/cd/E17952_01/refman-5.5-en/replication-features-max-allowed-packet.html
1
max_allowed_packet sets an upper limit on the size of any single message between the MySQL server and clients, including replication slaves.
此參數規定mysql服務端和客戶端之前的單個消息最大長度,在mysql主從同步時同樣有效。