replace into和insert into on duplicate key 区别
来源:互联网 发布:程序员杂志封面集 编辑:程序博客网 时间:2024/04/20 05:03
CREATE TABLE `test` (
`id` tinyint(3) unsigned NOT NULL auto_increment,
`name` char(10) NOT NULL default ”,
`dept` char(10) NOT NULL default ”,
`age` tinyint(3) unsigned NOT NULL default ‘0′,
PRIMARY KEY (`id`),
UNIQUE KEY `uni_key` (`name`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 |
mysql> select * from test;
+—-+———+————+—–+
| id | name | dept | age |
+—-+———+————+—–+
| 1 | wang | IT | 30 |
| 2 | hong | Accounting | 20 |
| 3 | gang | Sales | 40 |
| 4 | raymond | Service | 20 |
+—-+———+————+—–+
4 rows in set (0.00 sec)
mysql> replace into test (name,dept,age) values(‘gang’,'IT’,25);
Query OK, 2 rows affected (0.00 sec)
mysql> select * from test;
+—-+———+————+—–+
| id | name | dept | age |
+—-+———+————+—–+
| 1 | wang | IT | 30 |
| 2 | hong | Accounting | 20 |
| 5 | gang | IT | 25 |
| 4 | raymond | Service | 20 |
+—-+———+————+—–+
4 rows in set (0.00 sec)
mysql> replace into test (name,dept,age) values(‘test’,'IT’,25);
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+—-+———+————+—–+
| id | name | dept | age |
+—-+———+————+—–+
| 1 | wang | IT | 30 |
| 2 | hong | Accounting | 20 |
| 5 | gang | IT | 25 |
| 4 | raymond | Service | 20 |
| 6 | test | IT | 25 |
+—-+———+————+—–+
5 rows in set (0.00 sec)
mysql> replace into test (name,dept) values(‘hong’,'Sales’);
Query OK, 2 rows affected (0.00 sec)
mysql> select * from test;
+—-+———+———+—–+
| id | name | dept | age |
+—-+———+———+—–+
| 1 | wang | IT | 30 |
| 7 | hong | Sales | 0 |
| 5 | gang | IT | 25 |
| 4 | raymond | Service | 20 |
| 6 | test | IT | 25 |
+—-+———+———+—–+
5 rows in set (0.00 sec)
Replace:
当没有key冲突时,replace相当于普通的insert.
当与key冲突时,replace覆盖相关字段,同时auto_increment累加,其它字段填充默认值,可以理解为删除重复key的记录,新插入一条记录。
mysql> insert into test (name,dept,age) values(‘hong’,'Testing’,24)
-> on duplicate key update age=age+1;
Query OK, 2 rows affected (0.00 sec)
mysql> select * from test;
+—-+———+———+—–+
| id | name | dept | age |
+—-+———+———+—–+
| 1 | wang | IT | 30 |
| 7 | hong | Sales | 1 |
| 5 | gang | IT | 25 |
| 4 | raymond | Service | 20 |
| 6 | test | IT | 25 |
+—-+———+———+—–+
5 rows in set (0.00 sec)
mysql> insert into test (name,dept,age) values(‘hong’,'Manager’,24)
on duplicate key update age=100;
Query OK, 2 rows affected (0.00 sec)
mysql> select * from test;
+—-+———+———+—–+
| id | name | dept | age |
+—-+———+———+—–+
| 1 | wang | IT | 30 |
| 7 | hong | Sales | 100 |
| 5 | gang | IT | 25 |
| 4 | raymond | Service | 20 |
| 6 | test | IT | 25 |
+—-+———+———+—–+
5 rows in set (0.00 sec)
Insert into …on duplicate key:
当与key冲突时,只update相应字段值。
http://hi.baidu.com/jackbillow/blog/item/de9c4233bee7f3f41b4cffc8.html
- replace into和insert into on duplicate key 区别
- REPLACE INTO 和 INSERT INTO ... ON DUPLICATE KEY UPDATE SET ...
- replace into 和 insert into *** on duplicate key update
- mysql中insert into on duplicate key update 和replace into的区别
- mysql replace into和 insert into ... on duplicate update 区别
- insert into replace into 和insert into .. on duplicate key update ..
- INSERT INTO ON DUPLICATE KEY UPDATE 与 REPLACE INTO 和 INSERT IGNORE INTO
- mysql replace into,insert into ... on duplicate key
- Mysql replace into 与 insert into on duplicate key update 的区别
- Insert into ..... on duplicate key update ...与Replace into的区别
- Mysql中Insert into xxx on duplicate key update和REPLACE INTO使用
- Mysql中Insert into xxx on duplicate key update和REPLACE INTO使用
- Mysql replace into 与 insert into on duplicate key update 死锁和性能测试
- mybatis中使用replace into和 insert INTO … ON DUPLICATE KEY UPDATE批量操作
- mysql的replace into和INSERT INTO ON DUPLICATE KEY 的适用情况
- REPLACE与INSERT INTO ... ON DUPLICATE KEY总结
- REPLACE INTO 与 INSERT ... ON DUPLICATE KEY UPDATE
- MYSQL之REPLACE INTO和INSERT … ON DUPLICATE KEY UPDATE用法
- 通用性更强的双向链表
- 爱好就是一切,灵感源于内在世界,能力取决于知识渊博,魄力在于性格,
- JSP动作指令
- MySQL事件(ZZ)
- 无极性的电容能代替有极性的电容吗
- replace into和insert into on duplicate key 区别
- 认识自我,认知内心,认可自己
- 分布式系统浅析
- 跟我一起写 Makefile(转载)
- C++遍历文件夹
- RamDisk相关
- 采用数据库为Flex Tree组件的提供数据-XML数据生成
- 链表头文件C语言
- 链表实现文件C语言