实操MySQL+PostgreSQL批量插入更新insertOrUpdate

2022-07-22,,,,

目录
  • 二、postgres中insertorupdate代码实例
  • 四、usermapper.xml写法
    • 五、mysql中insertorupdate代码实例
      • 3、on duplicate key update
      • 4、replace into
      • 5、insert ignore into

    一、百度百科

    1、mysql

    mysql声称自己是最流行的开源数据库。lamp中的m指的就是mysql。构建在lamp上的应用都会使用mysql,如wordpress、drupal等大多数php开源程序。

    mysql最初是由mysql ab开发的,然后在2008年以10亿美金的价格卖给了sun公司,sun公司又在2010年被oracle收购。oracle支持mysql的多个版本:standard、enterprise、classic、cluster、embedded与community。其中有一些是免费下载的,另外一些则是收费的。

    其核心代码基于gpl许可,由于mysql被控制在oracle,社区担心会对mysql的开源会有影响,所以开发了一些分支,比如: mariadb和percona。

    2、postgresql

    postgresql标榜自己是世界上最先进的开源数据库。

    postgresql的一些粉丝说它能与oracle相媲美,而且没有那么昂贵的价格和傲慢的客服。

    最初是1985年在加利福尼亚大学伯克利分校开发的,作为ingres数据库的后继。postgresql是完全由社区驱动的开源项目。

    它提供了单个完整功能的版本,而不像mysql那样提供了多个不同的社区版、商业版与企业版。

    postgresql基于自由的bsd/mit许可,组织可以使用、复制、修改和重新分发代码,只需要提供一个版权声明即可。

    3、postgresql相对于mysql的优势

    (1)不仅仅是关系型数据库,还可以存储:

    array,不管是一位数组还是多为数组均支持json(hstore)和jsonb,相比使用text存储接送要高效很多

    (2)支持地理信息处理扩展

    (3)可以快速构建rest api

    (4)支持r-trees这样可扩展的索引类型,可以更方便地处理一些特殊数据。mysql 处理树状的设计会很复杂, 而且需要写很多代码, 而 postgresql 可以高效处理树结构。

    (5)更好的外部数据源支持

    (6)字符串没有长度限制

    等等...

    二、postgres中insertorupdate代码实例

    1、创建user表

    create table public.t_user (
        username varchar(100) not null,
        age int4 not null default 0,
        "password" varchar(100) null,
        deleted int4 null,
        created_time timestamp null
    );
    create unique index t_user_union_name_age_password on public.t_user using btree (username, password, age);

    2、简单的方式实现

    insert
        into
        public.t_user (username , password,age,created_time)
    values ('zs', '123', 18,now()), ('ls', '123456', 19,now()),('ww', '123', 20,now()) 
    on conflict (username, age,password) do update set username = excluded.username,age = excluded.age,password = excluded.password,created_time = excluded.created_time

    3、利用unnest函数实现

    insert
        into
        public.t_user (username , password,age,created_time)
    values (unnest(array['zs', 'ls', 'ww']), unnest(array['123', '123', '123456']),unnest(array[18, 19, 20]), unnest(array[now(), now(), now()])) 
    on conflict (username, age,password) do update set username = excluded.username,age = excluded.age,password = excluded.password,created_time = excluded.created_time

    4、如果数据已存在,就就什么也不做

    三、相关重点函数简介

    1、unnest(anyarray)

    unnest函数将输入的数组转换成一个表,这个表的每一列都代表相应的一个数组中的元素。
    如果unnest与其他字段一起出现在select中,就相当于其他字段进行了一次join。

    主要用于完成行转列的场景。

    insert on conflict实现postgresql插入更新特性。

    excluded虚拟表,其包含我们要更新的记录

    四、usermapper.xml写法

    <?xml version="1.0" encoding="utf-8" ?>
    <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.guor.dao.usermapper">
     
        <!-- 批量插入 -->
        <insert id="batchinsert" parametertype="java.util.hashmap">
             <include refid="batchinsertsql"></include>
        </insert>
     
        <sql id="batchinsertsql">
            insert into ${map.tableinfo.schemaname}.${map.tableinfo.tablename}
            (
            "table_id",
            "file_name",
            "create_time",
            <foreach collection="map.list.get(0)" index="key" item="value"
                     separator=",">
                "${key}"
            </foreach>
            )
            values
            <foreach collection="map.list" item="list" separator=",">
                (
                ${map.tableinfo.tableid},
                #{map.tableinfo.filename},
                now(),
                <foreach collection="list" index="key" item="value"
                         separator=",">
                    <choose>
                        <when test="map.varcharlist.contains(key)">
                            #{value}
                        </when>
                        <when test="map.datelist.contains(key)">
                            to_timestamp(#{value},'yyyy-mm-dd hh24:mi:ss')
                        </when>
                        <otherwise>
                            ${value}
                        </otherwise>
                    </choose>
                </foreach>
                )
            </foreach>
        </sql>
     
        <!-- 批量插入更新 -->
        <insert id="batchinsertorupdate" parametertype="java.util.hashmap">
            <include refid="batchinsertsql"></include>
            on conflict (
            file_name, table_id
            <if test="map.tableinfo.flag">
                , "id_number"
            </if>
            ) do update
            set
            "table_id" = excluded."table_id",
            "file_name" = excluded."file_name",
            "create_time" = excluded."create_time",
            <foreach collection="map.list.get(0)" index="key" separator=",">
                "${key}" = excluded."${key}"
            </foreach>
        </insert>
    </mapper>

    五、mysql中insertorupdate代码实例

    1、建表语句

    create table `t_user`  (
      `username` varchar(255) character set utf8 collate utf8_general_ci not null,
      `password` varchar(255) character set utf8 collate utf8_general_ci not null,
      `age` int(0) null default null,
      `address` varchar(255) character set utf8 collate utf8_general_ci null default null,
      `create_time` datetime(0) null default null,
      `update_time` datetime(0) null default null,
      `version` int(0) not null,
      unique index `user_union_index`(`username`, `password`, `age`) using btree
    ) engine = innodb character set = utf8 collate = utf8_general_ci row_format = dynamic;

    2、普通方式

    insert into t_user
    (username,password,age,create_time) 
    values('张三' ,'123456',18,now())
    on duplicate key update 
    username='张三',
    password='123456',
    create_time=now()

    3、on duplicate key update

    insert into on duplicate key update表示插入更新数据,当记录中有primarykey,或者unique索引的话,如果数据库已经存在数据,则用新数据更新(update),如果没有数据效果则和insert into一样。

    insert into t_user 
    (username,password,age,create_time,update_time,version)
    values( 'zs' ,'123',10,now(),now(),1) 
    ,( 'ls' ,'123456',20,now(),now(),1) 
    ,( 'ww' ,'123',30,now(),now(),1) 
    on duplicate key update 
    username= values(username)
    ,password=values(password)
    ,age=values(age)
    ,update_time=values(update_time)
    ,version = version + 1

    4、replace into

    replace into表示插入替换数据,当记录中有primarykey,或者unique索引的话,如果数据库已经存在数据,则用新数据替换(先delete再insert),如果没有数据效果则和insert into一样。

    replace into t_user 
    (username,password,age,create_time,update_time,version) 
    values 
    ( 'zs' ,'123',10,now(),now(),1) 

    5、insert ignore into

    insert ignore into表示尽可能的忽略冲突,暴力插入。

    insert ignore into t_user 
    (username,password,age,create_time,update_time,version) 
    values 
    ( 'zs' ,'123',10,now(),now(),1) ,
    ( '哪吒' ,'123',30,now(),now(),2) 

    6、小结

    insert into values 或 insert into select批量插入时,都满足事务的原子性与一致性,但要注意insert into select的加锁问题。
    replace into与insert into on duplicate key update都可以实现批量的插入更新,具体是更新还是插入取决与记录中的pk或uk数据在表中是否存在。

    如果存在,前者是先delete后insert,后者是update。
    insert ignore into会忽略很多数据上的冲突与约束,平时很少使用。

    到此这篇关于如何实现mysql + postgresql批量插入更新insertorupdate的文章就介绍到这了,更多相关mysql + postgresql批量插入更新insertorupdate内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

    《实操MySQL+PostgreSQL批量插入更新insertOrUpdate.doc》

    下载本文的Word格式文档,以方便收藏与打印。