目錄
- 一、百度百科
- 1、MySQL
- 2、PostgreSQL
- 3、PostgreSQL相對于MySQL的優勢
- 二、postgres中insertOrUpdate代碼實例
- 1、創建user表
- 2、簡單的方式實現
- 3、利用unnest函數實現
- 4、如果數據已存在,就就什么也不做
- 三、相關重點函數簡介
- 四、userMapper.xml寫法
- 五、MySQL中insertOrUpdate代碼實例
- 1、建表語句
- 2、普通方式
- 3、ON DUPLICATE KEY UPDATE
- 4、REPLACE INTO
- 5、INSERT IGNORE INTO
- 6、小結
一、百度百科
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 update set 和 and的區別
- MySQL UPDATE 語句的非標準實現代碼
- mysql update case 更新字段值不固定的操作
- Mysql update多表聯合更新的方法小結
- mysql事務select for update及數據的一致性處理講解
- MySQL UPDATE 語句一個“經典”的坑