SQL insert into statement writing method explanation

SQL insert into statement writing method explanation

Method 1: INSERT INTO t1(field1,field2) VALUE(v001,v002);

Explicitly insert only one Value

Method 2: INSERT INTO t1(field1,field2) VALUES(v101,v102),(v201,v202),(v301,v302),(v401,v402);

Method 2 is better than method 1 when inserting batch data.

[Special Note] When id is auto-increment, that is, id INT PRIMARY KEY AUTO_INCREMENT, when executing the insert into statement, all fields except id need to be listed (does it feel inconvenient? I hope MySQL will provide a simple method to mark this situation, because it is commonly used when testing data in the early days, and it is really troublesome to list all fields except id).

Method 3.1, INSERT INTO t2(field1,field2) SELECT colm1,colm2 FROM t1 WHERE ...

Here is a brief introduction. Since you can specify the columns to be inserted into talbe2 and obtain the data source through relatively complex query statements, it may be more flexible to use. However, we must also pay attention to the fact that when we specify the columns of the target table, we must fill in all non-empty columns, otherwise data insertion will not be possible. Another point that is prone to error is when we write it in the following abbreviated format:

Method 3.2: INSERT INTO t2 SELECT colm1,colm2,…… FROM t1

At this point, if we omit the columns of the target table, data will be inserted into all the columns of the target table by default, and the order of the columns after SELECT must be exactly the same as the definition order of the columns in the target table to complete the correct data insertion. This is a point that is easily overlooked and deserves attention.

[Special Note] Since the insert operation only roughly checks the [data type] of all fields in tables t1 and t2 in order, the [field name] is not checked. This is a double-edged sword, providing convenience but also carrying the potential for risk due to carelessness. During use, the order needs to be confirmed. It is recommended to use [Method 3.1] or [Method 4].

Method 4: INSERT INTO table name SET column name 1 = column value 1, column name 2 = column value 2,...;

However, using INSERT INTO SET, you cannot add data in batches. (Reference: Advantages of inserting data INSERT INTO SET in MySQL database)

This is the end of this article about how to write SQL insert into statements. For more relevant SQL insert into statement 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:
  • How to check if data exists before inserting in mysql
  • How to implement inserting a record when it does not exist and updating it if it exists in mysql
  • MySQL Practical Experience of Using Insert Statement
  • Use SQL statement to determine whether the record already exists before insert

<<:  Implementation steps of encapsulating components based on React

>>:  How to adjust the log level of nginx in Docker

Recommend

Solution to the cross-domain problem of SpringBoot and Vue interaction

Table of contents Browser Same Origin Policy 1. V...

In-depth explanation of currying of JS functions

Table of contents 1. Supplementary knowledge poin...

A brief discussion on MySQL event planning tasks

1. Check whether event is enabled show variables ...

CSS3 implementation example of rotating only the background image 180 degrees

1. Mental Journey When I was writing the cockpit ...

UDP DUP timeout UPD port status detection code example

I have written an example before, a simple UDP se...

HTML+CSS project development experience summary (recommended)

I haven’t updated my blog for several days. I jus...

Complete steps for using Nginx+Tomcat for load balancing under Windows

Preface Today, Prince will talk to you about the ...

What is jQuery used for? jQuery is actually a js framework

Introduction to jQuery The jQuery library can be ...

The implementation of event binding this in React points to three methods

1. Arrow Function 1. Take advantage of the fact t...

How to use the EXPLAIN command in SQL

In daily work, we sometimes run slow queries to r...

About MYSQL, you need to know the data types and operation tables

Data Types and Operations Data Table 1.1 MySQL ty...

Understanding of the synchronous or asynchronous problem of setState in React

Table of contents 1. Is setState synchronous? asy...

Learn more about MySQL indexes

1. Indexing principle Indexes are used to quickly...

Summary of the minesweeping project implemented in JS

This article shares the summary of the JS mineswe...

How to manage docker through UI

Docker is being used in more and more scenarios. ...