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

Analysis of Vue element background authentication process

Preface: Recently, I encountered a management sys...

Linux common commands chmod to modify file permissions 777 and 754

The following command is often used: chmod 777 文件...

Detailed explanation of MySQL foreign key constraints

Official documentation: https://dev.mysql.com/doc...

Nginx configures the same domain name to support both http and https access

Nginx is configured with the same domain name, wh...

How to insert video into HTML and make it compatible with all browsers

There are two most commonly used methods to insert...

Introduction to the use of em in elastic layout in CSS3: How many pixels is 1em?

I have been using CSS for a long time, but I have...

jQuery realizes the scrolling effect of table row data

This article example shares the specific code of ...

How to use css overflow: hidden (overflow hiding and clearing floats)

Overflow Hide It means hiding text or image infor...

Vue.js implements music player

This article shares the specific code of Vue.js t...

A brief introduction to the general process of web front-end web development

I see many novice students doing front-end develop...

How to build php7 with docker custom image

First, perform a simple Docker installation. To c...

How to remotely connect to the cloud server database using Navicat

It is very convenient to connect to a remote serv...

Solve the problem that vue project cannot carry cookies when started locally

Solve the problem that the vue project can be pac...

Uncommon but useful tags in Xhtml

Xhtml has many tags that are not commonly used but...