SQL statement INSERT INTO statement

Source: Internet
Author: User
Tags bulk insert

Way 1:insert into T1 (field1,field2) VALUE (' 001 ', ' 002 ');

The best practice is to explicitly insert each column, listed in the error, and the chance of missing the column will be reduced!

Way 2:insert into T1 (field1,field2) VALUES (' 001 ', ' 002 '), (' 003 ', ' 004 ') ...;

It is best to bulk insert data in bulk, but when the data is too large, it is best to BULK insert in batches!

Way 3:insert into T2 (field1,field2) SELECT col1,col2 from T1 WHERE ...;

To put it simply, it may be more flexible to use because you can specify columns to insert into talbe2, and data sources can be obtained through relatively complex query statements, but we must also note that when we specify columns for the target table, we must fill in all non-empty columns. Otherwise, you won't be able to insert the data, and there's a little bit more error-prone when we write the variant shorthand format for the following 3:

Variant of Mode 3: INSERT into T2 SELECT ID, name, address from T1

At this point, if we omit the column of the target table, then the data is inserted by default on all columns of the target table, and the order of the columns after the select must be exactly the same as the columns in the target table in order to complete the correct data insertion, which is a very easy to ignore place, worth noting.

SQL statement INSERT INTO statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.