mysql一次更新多条不一样的记录

  最近oschina上又有朋友问到了mysql中一次更新多条不一样的记录的方法,我知道的方法有两种,使用on duplicate key update语法和使用 replace into语法。
  这两个语法都须要主键索引或惟一索引支持,下面举例说明。
  测试用的表结构和数据
CREATE TABLE `t` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `c1` varchar(50) NOT NULL DEFAULT '',
  `c2` varchar(50) NOT NULL DEFAULT '',
  `c3` varchar(50) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `c1` (`c1`)
) ENGINE=InnoDB AUTO_INCREMENT=125 DEFAULT CHARSET=utf8 ;
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 html

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语法效果同样。例如: mysql

mysql> insert into t (id,c1,c2)values(1,20,30),(5,60,70) on duplicate key update c1=values(c1),c2=values(c2);
Query OK, 4 rows affected (0.00 sec)
Records: 2  Duplicates: 2  Warnings: 0

mysql> select * from t;
+----+----+----+----+
| id | c1 | c2 | c3 |
+----+----+----+----+
|  1 | 20 | 30 | 4  |
|  5 | 60 | 70 | 8  |
+----+----+----+----+
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 sql

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语法效果同样。例如:
  彻底替换两行记录
mysql> replace into t (id,c1) values(1,200),(5,600);
Query OK, 4 rows affected (0.00 sec)
Records: 2  Duplicates: 2  Warnings: 0

mysql> select * from t;                             
+----+-----+----+----+
| id | c1  | c2 | c3 |
+----+-----+----+----+
|  1 | 200 |    |    |
|  5 | 600 |    |    |
+----+-----+----+----+
2 rows in set (0.00 sec)

  不使用ID,使用惟一索引来替换记录
mysql> replace into t (c1,c2) values(200,3),(600,7);   
Query OK, 4 rows affected (0.00 sec)
Records: 2  Duplicates: 2  Warnings: 0

mysql> select * from t;                             
+-----+-----+----+----+
| id  | c1  | c2 | c3 |
+-----+-----+----+----+
| 127 | 200 | 3  |    |
| 128 | 600 | 7  |    |
+-----+-----+----+----+
2 rows in set (0.00 sec)
  效果是id也被替换掉了.

  当使用惟一索引,并重且给惟一索引这一列加了重复的值时
mysql> replace into t (id,c1) values(127,200),(128,200);
Query OK, 5 rows affected (0.00 sec)
Records: 2  Duplicates: 3  Warnings: 0

mysql> select * from t;
+-----+-----+----+----+
| id  | c1  | c2 | c3 |
+-----+-----+----+----+
| 128 | 200 |    |    |
+-----+-----+----+----+
1 row in set (0.00 sec)

  最后为何只剩一条记录了?插入(127,200)这一行前,会删掉id=127或c1=200的行,而后执行插入。插入(128,200)这一行前,会删掉id=128或c1=200的行,恰好前面插入的那一行中,c1=200,因此前面那一行也被删掉了,最后只留下了一行。 shell


一次最多能更新多少条记录?

  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 oracle

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主从同步时一样有效。
相关文章
相关标签/搜索