数据库操作,如果数据存在则update否则insert

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

在 SQL Server 中可以这样处理:

   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中可以这样处理:

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操作的数据表必须具有主键或唯一索引,否则会直接插入数据)。

例:表结构如下。

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条数据:

+----+----------+-----+------+
| 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');

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

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

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

而当更新数据的时候需要根据原来的值进行计算的时候,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    |
+----+----------+-----+------+

160133_XOr4_1459992.jpg

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




转载于:https://my.oschina.net/u/1459992/blog/209325

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值