Common writing examples for MySQL and Oracle batch insert SQL

Common writing examples for MySQL and Oracle batch insert SQL

For example:

Now we need to add User objects to the USER table in the database in batches

public class User{
    //Nameprivate String name;
    //Ageprivate Integer age;
    //Genderprivate Integer sex
}

Most people are familiar with MySQL and may think that batch-added SQL is written in this way, but this is not the case. This writing method is fine in MySQL, but in Oracle, it will cause an error.

MySQL writing:

INSERT INTO USER 
    (NAME,AGE,SEX)
VALUES
('val1_1', 'val1_2', 'val1_3'),
('val2_1', 'val2_2', 'val2_3'),
('val3_1', 'val3_2', 'val3_3');

Oracle writing:

//Multiple single inserts INSERT INTO USER (NAME,AGE,SEX) VALUES ('val1_1', 'val1_2', 'val1_3');
INSERT INTO USER (NAME,AGE,SEX) VALUES ('val2_1', 'val2_2', 'val2_3');
INSERT INTO USER (NAME,AGE,SEX) VALUES ('val3_1', 'val3_2', 'val3_3');

//Batch insert INSERT ALL
   INTO USER (NAME,AGE,SEX) VALUES ('val1_1', 'val1_2', 'val1_3')
   INTO USER (NAME,AGE,SEX) VALUES ('val2_1', 'val2_2', 'val2_3')
   INTO USER (NAME,AGE,SEX) VALUES ('val3_1', 'val3_2', 'val3_3')
SELECT 1 FROM DUAL;

It can be found that both Oracle writing methods are relatively troublesome, and batch insertion does not reduce the number of inserted column names at all. In addition, another troublesome thing is that in enterprise development, a software system may need to support multiple databases. Therefore, this newly added operation needs to adapt to two databases and maintain two sets of SQL, which greatly increases the development cost.

So is there a universal way of writing it? The answer is yes.

General writing:

INSERT INTO USER (NAME,AGE,SEX)
    select ('val1_1', 'val1_2', 'val1_3') from dual union all
    select ('val2_1', 'val2_2', 'val2_3') from dual union all
    select ('val3_1', 'val3_2', 'val3_3') from dual

This way, you can simplify the process and maintain one less set of SQL, achieving the best of both worlds.

Below is the code for various writing methods in XML files.

<!--MySQL batch insert-->
<insert id="batchInsertUser" databaseId="mysql">
   INSERT INTO USER 
            (NAME,AGE,SEX)
   VALUES
    <foreach collection="userList" index="index" item="user" separator=",">
            (#{user.name},#{user.age},#{user.sex})
  </foreach>
</insert>
<!--Oracle batch insert-->
<insert id="batchInsertUser" databaseId="oracle">
  BEGIN
  <foreach collection="userList" index="index" item="user" separator=";">
   INSERT INTO USER 
            (NAME,AGE,SEX)
   VALUES
            (#{user.name},#{user.age},#{user.sex})
  </foreach>
  ;END;
</insert>

Carefully observe the writing methods of MySQL and Oracle, because MySQL supports the above-mentioned direct insertion of multiple data after VALUES. The foreach tag only needs to loop through the contents after VALUES (); Oracle does not support this writing method and therefore needs to loop through the entire INSERT statement.

<!--General batch insert-->
<insert id="batchInsertUser" databaseId="mysql">
   INSERT INTO USER 
            (NAME,AGE,SEX)
  <foreach collection="userList" index="index" item="user" separator="union all">
     SELECT
            (#{user.name},#{user.age},#{user.sex})
     FROM DUAL
  </foreach>
</insert>

Summarize

This is the end of this article about the general writing method of MySQL and Oracle batch insert SQL. For more relevant MySQL and Oracle batch insert SQL content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Problems and solutions when replacing Oracle with MySQL
  • MySQL to Oracle real-time data synchronization

<<:  A brief discussion on HTML doctype and encoding

>>:  Slot arrangement and usage analysis in Vue

Recommend

Introduction to Jenkins and how to deploy Jenkins with Docker

1. Related concepts 1.1 Jenkins Concepts: Jenkins...

css add scroll to div and hide the scroll bar

CSS adds scrolling to div and hides the scroll ba...

How to use the Linux md5sum command

01. Command Overview md5sum - Calculate and verif...

Things to note when writing self-closing XHTML tags

The img tag in XHTML should be written like this:...

Vue custom table column implementation process record

Table of contents Preface Rendering setTable comp...

Detailed explanation of Mysql's concurrent parameter adjustment

Table of contents Query cache optimization Overvi...

Summary of pitfalls in virtualbox centos7 nat+host-only networking

Table of contents 1. Problem Background 2. What a...

How to decrypt Linux version information

Displaying and interpreting information about you...

Summary of event handling in Vue.js front-end framework

1. v-on event monitoring To listen to DOM events,...

Several methods of implementing carousel images in JS

Carousel The main idea is: In the large container...

A complete list of meta tag settings for mobile devices

Preface When I was studying the front end before,...

JavaScript explains the encapsulation and use of slow-motion animation

Implementing process analysis (1) How to call rep...

Example explanation of alarm function in Linux

Introduction to Linux alarm function Above code: ...

JavaScript to achieve fireworks effects (object-oriented)

This article shares the specific code for JavaScr...