Three ways to write insert into statement

方式1、 INSERT INTO t1(field1,field2) VALUE(v001,v002);           

// Explicitly insert only one Value

 

方式2、 INSERT INTO t1(field1,field2) VALUES(v101,v102),(v201,v202),(v301,v302),(v401,v402);

// Method 2 is better than method 1 when inserting bulk data.

 

方式3.1、  INSERT INTO t2(field1,field2) SELECT col1,col2 FROM t1 WHERE ……

Let me briefly say here, because you can specify the columns to be inserted into talbe2, and you can obtain the data source through relatively complex query statements, it may be more flexible to use, but we must also pay attention that we are specifying the target table. When adding columns, be sure to fill in all non-empty columns, otherwise data insertion will not be possible, and there is a more error-prone place, when we write it in the following shorthand format:

 

方式3.2、  INSERT INTO t2 SELECT id, name, address FROM t1

At this time, 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 the SELECT must be exactly the same as the definition order of the columns in the target table to complete the correct operation. Data insertion, which is an easily overlooked place, is worth noting.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326299170&siteId=291194637