How can multiple rows be inserted into a table in MySQL?

To insert multiple rows into a MySQL table, you can use the multiple value insertion method with the INSERT statement. The specific steps are as follows:

  1. Please specify the table name, column names, and data to be inserted.
  2. Use the INSERT INTO statement to specify the table name and column names, and specify the data to be inserted in the VALUES clause.
  3. Use multiple parentheses in the VALUES clause, with each set containing a group of data to be inserted.
  4. Ensure that the order of the values in each data set corresponds to the order of the columns, ensuring that the data is inserted in the correct order.

Here is an example:

Suppose there is a table called students, consisting of columns for id, name, and age, and we need to insert data for two students.

Table structure:
CREATE TABLE students (
id INT NOT NULL PRIMARY KEY,
name VARCHAR(50),
age INT
);

Insert two rows of data into the students table:
INSERT INTO students (id, name, age) VALUES
(1, ‘John’, 18),
(2, ‘Alice’, 20);

After executing the above statement, two records will be inserted into the students table – one with id 1, name ‘John’, and age 18, and the other with id 2, name ‘Alice’, and age 20.

bannerAds