mysql迁移PG库 主键、唯一处理、批量修改

1 主键处理:

js 复制代码
主键字段,mybatis中不可以传递null,在insert 中去嗲主键ID的插入;

2 唯一性校验的处理

mysql: 不需要指定唯一条件,会根据插入结果判断

xml 复制代码
<update id="updateSequenceDetail" >
    INSERT INTO erp_sequence_detail (sequence_id, period_year, period_month, current_number)
    VALUES (#{sequenceId}, #{periodYear}, #{periodMonth}, #{currentNumber})
    ON DUPLICATE KEY UPDATE
    current_number = #{currentNumber};
</update>

pg: 需要传递唯一约束

xml 复制代码
--1 使用约束名
ON CONFLICT ON CONSTRAINT uk_seq_detail

-- 2 使用约束字段
ON CONFLICT (sequence_id, period_year, period_month)

修改后的sql:
<update id="updateSequenceDetail" >
    INSERT INTO erp_sequence_detail (sequence_id, period_year, period_month, current_number)
    VALUES (#{sequenceId}, #{periodYear}, #{periodMonth}, #{currentNumber})
    ON CONFLICT (sequence_id, period_year, period_month)
    DO UPDATE SET current_number = #{currentNumber}
</update>

3 pg添加唯一索引

xml 复制代码
-- 创建约束时指定名称
ALTER TABLE erp_sequence_detail 
ADD CONSTRAINT uk_seq_detail 
UNIQUE (sequence_id, period_year, period_month);

4 批量更新的写法

mysql:

xml 复制代码
<update id="batchUpsertLocationByStockIn">
    UPDATE erp_warehouse_location AS e
    JOIN (
        <foreach collection="subList" item="item" separator="UNION ALL">
            SELECT #{item.locationId} AS location_id,
                   #{item.tenantId} AS tenant_id,
                   #{item.skuId} AS sku_id,
                   #{item.quantity} AS change_quantity
            FROM DUAL
        </foreach>
    ) AS t
   ON e.location_id = t.location_id and e.tenant_id = t.tenant_id and e.sku_id = t.sku_id
    SET
        -- 修改总库存,允许数量为负数
        e.quantity = e.quantity + t.change_quantity;
</update>

pg库:

xml 复制代码
<update id="batchUpsertLocationByStockIn">
    UPDATE erp_warehouse_location AS e
    SET quantity = e.quantity + t.change_quantity
    FROM (
        VALUES
        <foreach collection="subList" item="item" separator=",">
            (#{item.locationId}, #{item.tenantId}, #{item.skuId}, #{item.quantity})
        </foreach>
    ) AS t(location_id, tenant_id, sku_id, change_quantity)
    WHERE e.location_id = t.location_id 
      AND e.tenant_id = t.tenant_id 
      AND e.sku_id = t.sku_id
</update>

5 find_in_set 不支持

mysql:

xml 复制代码
<update id="updateDeptChildrenStatus">
    update sys_dept set status = #{status} where find_in_set(#{deptId}, ancestors) and tenant_id = #{tenantId}
</update>

pg库:

xml 复制代码
<!-- 1 字符串包含判断 -->
<update id="updateDeptChildrenStatus">
    UPDATE sys_dept 
    SET status = #{status} 
    WHERE ancestors LIKE '%' || #{deptId} || '%' 
      AND tenant_id = #{tenantId}
</update>

<!-- 2 正则包含判断 -->
<update id="updateDeptChildrenStatus">
    UPDATE sys_dept 
    SET status = #{status} 
    WHERE ancestors ~ ('(^|,)' || #{deptId} || '(,|$)')
      AND tenant_id = #{tenantId}
</update>

<!-- 3 最精确,需要标准逗号分隔 -->
<update id="updateDeptChildrenStatus">
    UPDATE sys_dept 
    SET status = #{status} 
    WHERE #{deptId} = ANY(string_to_array(ancestors, ','))
      AND tenant_id = #{tenantId}
</update>
相关推荐
ovensi18 小时前
Docker+NestJS+ELK:从零搭建全链路日志监控系统
后端·nestjs
武子康18 小时前
大数据-184 Elasticsearch Doc Values 机制详解:列式存储如何支撑排序/聚合/脚本
大数据·后端·elasticsearch
四月__18 小时前
http八股
后端
沐森18 小时前
rust并发
后端
喵个咪18 小时前
开箱即用的 GoWind Admin|风行,企业级前后端一体中后台框架:Casbin集成指南
后端·go
墨守城规18 小时前
FutureTask源码分析
后端
梨子同志19 小时前
Java 基础语法详解
后端
bcbnb19 小时前
详细教程:iOS应用中Swift代码混淆步骤与工具推荐
后端
气π19 小时前
【JavaWeb】——(若依 + AI)-基础学习笔记
java·spring boot·笔记·学习·java-ee·mybatis·ruoyi
expect7g19 小时前
Paimon源码解读 -- Compaction-8.专用压缩任务
大数据·后端·flink