数据库操做,若是数据存在则update不然insert

向表中插入数据:1.判断该条数据是否存在。2.若是存在则更新,若是不存在则插入。
mysql

在 SQL Server 中能够这样处理:sql

   if not exists (select 1 from t where id = 1)
      insert into t(id, update_time) values(1, getdate())
   else
      update t set update_time = getdate() where id = 1

在Mysql中能够这样处理:
ui

1. replace into tbl_name(col_name, ...) values(...)
2. replace into tbl_name(col_name, ...) select ...
3. replace into tbl_name set col_name=value, ...

replace into和insert into功能相似,区别是当使用replace into操做已存在的数据时(根据主键和惟一索引判断),会先删掉原有数据,再插入一条(注意:使用replace into操做的数据表必须具备主键或惟一索引,不然会直接插入数据)。code

例:表结构以下。
索引

mysql> show create table uid;
+-------+----------------------------------------------------------------------
-------------------------------------------------------------------------------
---------------------------------------------------------------------------+
| Table | Create Table

                                                                           |
+-------+----------------------------------------------------------------------
-------------------------------------------------------------------------------
---------------------------------------------------------------------------+
| uid   | CREATE TABLE `uid` (
  `id` int(3) NOT NULL auto_increment,
  `name` char(10) NOT NULL,
  `age` int(2) NOT NULL,
  `sex` enum('m','w') default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------
-------------------------------------------------------------------------------
---------------------------------------------------------------------------+

表中有3条数据:rem

+----+----------+-----+------+
| id | name     | age | sex  |
+----+----------+-----+------+
|  1 | niubao   |  30 | m    |
|  2 | pidaihou |  27 | w    |
|  3 | pidaihou |   1 | w    |
+----+----------+-----+------+

replace into uid(id,name,age,sex) values(1,'niubao',10,'m');get

replace into uid set id=1,name='niubao',age=10,sex='m';table

replace into uid(id,name,age,sex) select 1,'niubao',10,'m';
class

这三种写法均可以把id=1的数据中age修改成10。date

而当更新数据的时候须要根据原来的值进行计算的时候,replace into是不能知足要求的,这时候可使用insert into的高级应用:

insert into uid set id=1,name='niubao',age=20,sex='m' on duplicate key update age=age+10;

这样执行的时候发现id=1的记录存在,则会执行age=age+10操做。

以上全部状况都是创建在表中有主键id或惟一索引时,当表中即存在主键id,又存在惟一索引的时候,乐子就大了。

例:表结构以下(name字段增长了惟一索引)

   +-------+----------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------+
| uid   | CREATE TABLE `uid` (
  `id` int(3) NOT NULL auto_increment,
  `name` char(10) NOT NULL,
  `age` int(2) NOT NULL,
  `sex` enum('m','w') default NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------+
+----+----------+-----+------+
| id | name     | age | sex  |
+----+----------+-----+------+
|  1 | niubao   |  80 | m    |
|  2 | pidaihou |  27 | w    |
|  3 | dog      |   1 | w    |
+----+----------+-----+------+

能够看到,受影响记录条数为3,原表中id为1和2的两条数据被删掉了,再插入了一条id=1的新数据,so你们用的时候必定要注意表结构哦。。