Oracle与MySQL区别总结一:insert 保存的异同

通常情况下insert语句的写法为 insert into tablename values (a,b);

区别之处:

1oracle中使用如下语句

1.1方式一

该方式特点是能插如值是固定的多条数据
insert all
 
into test01 values(1,'a')
 
into test01 values(2,'b')
 
select 1 from dual; --这一行不能去掉

1.2方式二

该方式特点是:能插入一些值不是固定的多条数据.可以吧其他表中的数据批量保存到这张表中来
insert into test01 (id,line1)

       select id,line1 from test02;

1.3方式三

放在begin end里面如下:

begin
insert into test01 (id,line1) values (01,'line01');
insert into test01 (id,line1) values (01,'line01');
insert into test01 (id,line1) values (01,'line01');
insert into test01 (id,line1) values (01,'line01');
end;

这个可以在Mybatis的sql中优化为

<insert id="insertRoleInfoList"  parameterType="java.util.Map">
 
   <foreach collection="roleInfoList" item="userRoleInfo" index="index" open="begin" close=";end;" separator=";">
      insert into base_role_demo (roleid,rolecode,roledesc,remark,recordercode,recorderdesc,usecorpcode,usecorpdesc)
      values(ROLEDEMO_CORP_SEQ.Nextval,#{userRoleInfo.rolecode},#{userRoleInfo.roledesc},#{userRoleInfo.remark},
      #{recordercode},#{recorderdesc},#{userRoleInfo.usecorpcode},#{userRoleInfo.usecorpdesc})
    </foreach>
  
  </insert>

2mysql使用如下语句

insert into test01 (id,line1) values (01,'line01'),(02,'line02');

这种连写的方式可以同时添加多条

接下来说一说关于foreach 语句的区别

主要不同点在于foreach标签内separator属性的设置问题:

  1. separator设置为","分割时,最终拼接的代码形式为:insert into table_name (a,b,c) values (v1,v2,v3) ,(v4,v5,v6) ,...
  2. separator设置为"union all"分割时,最终拼接的代码形式为:insert into table_name (a,b,c) values (v1,v2,v3) union all (v4,v5,v6) union all...

oracle中:

<insert id="inserData" parameterType="com.test.aaa.Bac">
    insert into table_name (name, adress, age) values <foreach collection="list" item="item" index="index" separator="union all">
            (select #{item.name},
                    #{item.adress},
                    #{item.age}
                from dual   )
        </foreach>
</insert>

MySQL中:

<insert id="inserData" parameterType="com.test.aaa.Bac">
    insert into table_name (name, adress, age)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (   #{item.name},  #{item.adress},  #{item.age}  )
        </foreach>
</insert>

猜你喜欢

转载自blog.csdn.net/Alex_81D/article/details/89471655