insert or replace:如果不存在就插入,存在就更新
insert or ignore:如果不存在就插入,存在就忽略
只对UNIQUE约束的字段起作用。

举例:
建表:
CREATE TABLE TEST(id BIGINT, name text, birthday datetime, PRIMARY KEY(id, name));
或者CREATE TABLE TEST (id BIGINT, name text, birthday datetime, UNIQUE(id, name));
插入记录:
insert into test values (10,'aa','2010-01-01 01:01:01');
insert into test values (11,'bb','2011-01-01 01:01:01');
insert into test values (12,'cc','2012-01-01 01:01:01');

依次执行以下语句看结果:
1、insert or replace into test values (10,'jj','2016-01-01 01:01:01');//不存在,插入
id          name        birthday           
----------  ----------  -------------------
10          aa          2010-01-01 01:01:01
11          bb          2011-01-01 01:01:01
12          cc          2012-01-01 01:01:01
10          jj          2016-01-01 01:01:01

2、insert or replace into test values (11,'bb','2016-01-01 01:01:01');//存在,更新
id          name        birthday           
----------  ----------  -------------------
10          aa          2010-01-01 01:01:01
12          cc          2012-01-01 01:01:01
10          jj          2016-01-01 01:01:01
11          bb          2016-01-01 01:01:01

3、insert or ignore into test values (12,'xx','2017-01-01 01:01:01');//不存在,插入
id          name        birthday           
----------  ----------  -------------------
10          aa          2010-01-01 01:01:01
12          cc          2012-01-01 01:01:01
10          jj          2016-01-01 01:01:01
11          bb          2016-01-01 01:01:01
12            xx            2017-01-01 01:01:01

4、insert or ignore into test values (12,'xx','2018-01-01 01:01:01');//存在,忽略
id          name        birthday           
----------  ----------  -------------------
10          aa          2010-01-01 01:01:01
12          cc          2012-01-01 01:01:01
10          jj          2016-01-01 01:01:01
11          bb          2016-01-01 01:01:01
12          xx          2017-01-01 01:01:01

sqlite "insert or replace" 和 "insert or ignore" 用法的更多相关文章

  1. mysql 数据库插入语句之insert into,replace into ,insert ignore

    近期才发现mysql的插入语句竟然有如此多的使用方法,这里拿来分享一下. ①关于insert into : insert into table_name values(); insert into t ...

  2. sqlite中的replace、insert、update之前的区别

    本文转自http://www.ithao123.cn/content-933827.html,在此感谢作者 android数据库操作,有两种方式,一种用android提供给我们的数据库操作函数inse ...

  3. Sqlite执行insert or ignore 或insert or replace语句。

    Sqlite执行insert or ignore 或insert or replace语句. ,); ,); 上面的第一条语句是每次执行时,如果不存在,则添加,如果存在,则更新. 上面的第二条语句是每 ...

  4. MySQL中的insert ignore into, replace into等的一些用法小结(转)

    MySQL中的insert ignore into, replace into等的一些用法总结(转) 在MySQL中进行条件插入数据时,可能会用到以下语句,现小结一下.我们先建一个简单的表来作为测试: ...

  5. MYSQL开发性能研究——INSERT,REPLACE,INSERT-UPDATE性能比较

    一.为什么要有这个实验 我们的系统是批处理系统,类似于管道的架构.而各个数据表就是管道的两端,而我们的程序就类似于管道本身.我们所需要做的事情无非就是从A表抽取数据,经过一定过滤.汇总等操作放置到B表 ...

  6. replace和insert的语句插入(转)

    SELECT INTO 和 INSERT INTO SELECT 两种表复制语句 Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) valu ...

  7. Mysql INSERT、REPLACE、UPDATE的区别

    用于操作数据库的SQL一般分为两种,一种是查询语句,也就是我们所说的SELECT语句,另外一种就是更新语句,也叫做数据操作语句.言外之意,就是对数据进行修改.在标准的SQL中有3个语句,它们是INSE ...

  8. 转载:mysql 操作总结 INSERT和REPLACE

    转自:http://www.jb51.net/article/19411.htm   用于操作数据库的SQL一般分为两种,一种是查询语句,也就是我们所说的SELECT语句,另外一种就是更新语句,也叫做 ...

  9. spring boot:thymeleaf模板中insert/include/replace三种引用fragment方式的区别(spring boot 2.3.3)

    一,thymeleaf模板中insert/include/replace三种引用fragment方式的区别 insert: 把整个fragment(包括fragment的节点tag)插入到当前节点内部 ...

随机推荐

  1. Java中随机数生成的问题

    [生成随机数序列] 我们只能利用Math.random()方法只能生成一个在[0,1)之间的double类型浮点数. 但如果我们想要生成[min, max]之间的随机整数时该怎么办呢? 此时可以用: ...

  2. Linux 文件目录结构

    /           文件系统的入口,最高一级目录 /bin           存放系统所需的基本命令 /boot     包含Linux内核及系统引导程序所需要的文件 ( 如连接文件,镜像文件等 ...

  3. mvc 过滤器篇

    1,重写类作为特性 2 写一个controller ,让其他controller集成 四种过滤器 1身份验证过滤器 2异常处理过滤器 3行为过滤器 4结果过滤器

  4. ELM:ELM基于近红外光谱的汽油测试集辛烷值含量预测结果对比—Jason niu

    %ELM:ELM基于近红外光谱的汽油测试集辛烷值含量预测结果对比—Jason niu load spectra_data.mat temp = randperm(size(NIR,1)); P_tra ...

  5. Lunch War with the Donkey CSU - 2084

    Jingze is a big figure in California State University for his stubbornness. Because of his new failu ...

  6. csrf技巧

    Burpsuite,在数据包处右键,Engagement tools – Generate CSRF PoC,可生成csrf payload

  7. 【Spring Boot】构造、访问Restful Webservice与定时任务

    Spring Boot Guides Examples(1~3) 参考网址:https://spring.io/guides 创建一个RESTful Web Service 使用Eclipse 创建一 ...

  8. 超出JavaScript安全整数限制的数字计算-BigInt

    JavaScript中的基本数据类Number是双精度浮点数,它可以表示的最大安全范围是正负9007199254740991,也就是2的53次方减一,在浏览器控制台分别输入Number.MAX_SAF ...

  9. BZOJ-3-1010: [HNOI2008]玩具装箱toy-斜率优化DP

    dp[i]=min(dp[j]+(sum[i]-sum[j]+i-j-1-L)^2) (j<i) 令f[i]=sum[i]+i,c=1+l 则dp[i]=min(dp[j]+(f[i]-f[j] ...

  10. python学习—几个简单小程序

    1. 输出1到100的偶数 #!/usr/bin/env python # -*- coding:utf-8 -*- #定义初始值 start=1 while True: #判断start的值若其为5 ...