voltdb 记录存在时候更新,不存在时候插入

Wesley13
• 阅读 563
UPSERT
UPSERT — Either inserts new rows or updates existing rows depending on the primary key value.
Syntax
UPSERT INTO table-name [( column-name [,...] )] VALUES ( value-expression [,...] )
UPSERT INTO table-name [( column-name [,...] )] SELECT select-expression

Description
The UPSERT statement has the same syntax as the INSERT statement and will perform the same function,assuming a record with a matching primary key does not already exist in the database. If such a record does exist, UPSERT updates the existing record with the new column values. Note that the UPSERT statement can only be executed on tables that have a primary key.
UPSERT has the same two forms as the INSERT statement: UPSERT INTO... VALUES and UPSERT
INTO... SELECT. The UPSERT statement also has similar constraints and limitations as the INSERT
statement with regards to joining partitioned tables and overly complex SELECT clauses. (See the description of the INSERT statement for details.)
However, UPSERT INTO... SELECT has an additional limitation: the SELECT statement must produce
deterministically ordered results. That is, the query must not only produce the same rows, they must be in
the same order to ensure the subsequent inserts and updates produce identical results.
Examples
The following examples use two tables, Employee and Manager, both of which define the column emp_id
as a primary key. In the first example, the UPSERT statement either creates a new row with the specified
values or updates an existing row with the primary key 145303.

UPSERT INTO employee (emp_id, lastname, firstname, title, department)
VALUES (145303, 'Public', 'Jane', 'Manager', 'HR');

The next example copies records from the Employee table to the Manager table, if the employee's title
is "Manager". Again, new records will be created or existing records updated depending on whether the
employee already has a record in the Manager table. Notice the use of the primary key in an ORDER BY
clause to ensure deterministic results from the SELECT statement.

UPSERT INTO Manager (emp_id, lastname, firstname, title, department)
SELECT * from Employee WHERE title='Manager' ORDER BY emp_id;
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
SQL UPDATE 语句:更新表中的记录语法及案例剖析
SQLUPDATE语句UPDATE语句用于更新表中的记录。SQLUPDATE语句UPDATE语句用于更新表中已存在的记录。SQLUPDATE语法UPDATE_table\_name_SET_column1_\_valu
Wesley13 Wesley13
3年前
mysql存在则更新,不存在则插入
INSERTINTOONDUPLICATEKEYUPDATE与REPLACEINTO,两个命令可以处理重复键值问题,在实际上它之间有什么区别呢?前提条件是这个表必须有一个唯一索引或主键。unique1、REPLACE发现重复的先删除再插入,如果记录有多个字段,在插入的时候如果有的字段没有赋值,那么新插入的记录这些字段
Easter79 Easter79
3年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Stella981 Stella981
3年前
Android蓝牙连接汽车OBD设备
//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");
Stella981 Stella981
3年前
Mybatisd对MySQL批量插入、批量更新及批量删除语句
1、批量插入<insertid"insertBatch"parameterType"java.util.List"insertintot_student(name,age,class)values<forea
Wesley13 Wesley13
3年前
Mysql 插入记录时检查记录是否已经存在,存在则更新,不存在则插入记录SQL
我们在开发数据库相关的逻辑过程中,经常检查表中是否已经存在这样的一条记录,如果存在则更新或者不做操作,如果没有存在记录,则需要插入一条新的记录。这样的逻辑固然可以通过两条sql语句完成。SELECTCOUNT()FROMxxxWHEREIDxxx;if(x0)INSERTI
Wesley13 Wesley13
3年前
mysql 插入数据
简单用法:insertintotb\_name(字段1,字段2,.........)values(值1,值2,.....)注意,字段个数必须和值的个数一致。字符用引号引起来,数字不用,插入空值使用null批量插入:insertintotb\_name(字段1,字段2,.........)values(值1,值2,.....
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究