Mysql inserts data in batches, mysql inserts data

Source: Internet
Author: User

Mysql inserts data in batches, mysql inserts data

What should I do if MySQL inserts multiple records using INSERT? The following describes how to INSERT multiple records using INSERT in MySQL for your reference.

If you see this title, you may ask, is there anything you can say about it? Won't you be able to INSERT multiple records after multiple INSERT statements are called! However, using this method increases the load on the server, because every SQL Server executes the same SQL Analysis and Optimization operations. Fortunately, MySQL provides another solution, that is, using an INSERT statement to INSERT multiple records. This is not a standard SQL syntax, so it can only be used in MySQL.

Insert into users (name, age)

VALUES ('Yao Ming ', 25), ('bill Gates', 50), ('marker', 600 );

The preceding INSERT statement inserts three records into the users table consecutively. It is worth noting that the VALUES of each record must be placed in a pair (…) after VALUES in the preceding INSERT statement (...) "," Is used in the middle. Suppose there is a table table1

Create table table1 (n INT );

If you want to insert five records into Table 1, the following statement is incorrect:

Insert into table1 (I) VALUES (1, 2, 3, 4, 5 );

MySQL will throw the following error

ERROR 1136: Column count doesn't match value count at row 1

The correct statement is as follows:

Insert into t able1 (I) VALUES (1), (2), (3), (4), (5 );

Of course, this statement can also omit the column name, so that the number of values in each pair of brackets must be consistent, and this number must be consistent with the number of columns. For example:

Insert into t able1 VALUES (1), (2), (3), (4), (5 );

Related Article

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.