背景:某天,我老婆蛋,發了數據庫的博文給我看,因爲之前一直沒有仔細研究過replace into 與 on duplicate key update區別,此次燃起了我的好奇心,故作了驗證了。
以下是在mysql 5.1.50驗證的
1. 建表:
mysql> CREATE TABLE `check_t1` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` varchar(10) DEFAULT NULL,
`c` varchar(10) NOT NULL DEFAULT '',
PRIMARY KEY (`a`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2. 插入數據:
insert into check_t1(b,c) values('b1','c1'),('b2','c2'),('b3','c3');
mysql> show create table check_t1;
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| check_t1 | CREATE TABLE `check_t1` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` varchar(10) DEFAULT NULL,
`c` varchar(10) NOT NULL DEFAULT '',
PRIMARY KEY (`a`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
3. replace into:
mysql> replace into check_t1(a,b) value(1,'a11');
Query OK, 2 rows affected (0.00 sec)
mysql> select * from check_t1;
+---+------+----+
| a | b | c |
+---+------+----+
| 1 | a11 | |
| 2 | b2 | c2 |
| 3 | b3 | c3 |
+---+------+----+
mysql> show create table check_t1;
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| check_t1 | CREATE TABLE `check_t1` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` varchar(10) DEFAULT NULL,
`c` varchar(10) NOT NULL DEFAULT '',
PRIMARY KEY (`a`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
mysql> insert into check_t1(b,c) value('b4','c4');
Query OK, 1 row affected (0.00 sec)
mysql> select * from check_t1;
+---+------+----+
| a | b | c |
+---+------+----+
| 1 | a11 | |
| 2 | b2 | c2 |
| 3 | b3 | c3 |
| 4 | b4 | c4 |
+---+------+----+
所以當與主鍵衝突時,replace覆蓋相關字段,其它字段填充默認值,可以理解爲刪除重複key的記錄,新插入一條記錄,一個delete原有記錄再insert的操作,但自增長值不變。
4.insert .. on deplicate update
mysql> insert into check_t1(a,b) value(3,'b33') on duplicate key update b='b33';
Query OK, 2 rows affected (0.00 sec)
mysql> show create table check_t1;
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| check_t1 | CREATE TABLE `check_t1` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` varchar(10) DEFAULT NULL,
`c` varchar(10) NOT NULL DEFAULT '',
PRIMARY KEY (`a`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 |
mysql> select * from check_t1;
+---+------+----+
| a | b | c |
+---+------+----+
| 1 | a11 | |
| 2 | b2 | c2 |
| 3 | b33 | c3 |
| 4 | b4 | c4 |
+---+------+----+
由此可看當主鍵有時,只執行後面的udate操作語句,可以理解爲刪除重複key的記錄,新插入一條記錄,一個delete原有記錄再insert的操作,但自增長值不變。
5.總結:
insert .. on deplicate udpate保留了所有字段的舊值,再覆蓋然後一起insert進去,而replace沒有保留舊值,直接刪除再insert新值。從底層執行效率上來講,replace要比insert
.. on deplicate update效率要高,但是在寫replace的時候,字段要寫全,防止老的字段數據被刪除。