replace table VS insert into table on depulicate

     看标题就知道,我接下来要干什么了。呵呵。。今天无意中发现使用Replace原来可以这么简单的。呵呵。先看下,我从网上找过来的资料。

资料来源:http://blog.chinaunix.net/u2/61329/showart_494666.html

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累加,其它字段填充默认值。

 

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相应字段值。

小结:

      应用UNIQUE KEY ‘aa’ (‘字段A’,'字段B’…)的形式可以使某一条件的字段是唯一的,用这方面可以简化一些代码的编写。把相关的工作交给DBA处理。

一条评论 »

  1. qinyf said,

    二月 3, 2009 at 5:42 下午

    在有foreign key delete cascade的时候,replace有可能触发级联的delete操作,而insert on duplicate key update不会。这是个很危险的隐含操作,如果没外键,还好。

RSS feed for comments on this post · TrackBack URL

发表评论

You must be logged in to post a comment.