Showing posts with label MySQL INSERT INTO statement with multiple VALUES. Show all posts
Showing posts with label MySQL INSERT INTO statement with multiple VALUES. Show all posts

Thursday, 1 June 2023

MySql Multiple Insert

Yes, MySQL supports the INSERT INTO statement with multiple VALUES clauses. This allows you to insert multiple rows of data into a table in a single statement.


The syntax for the INSERT INTO statement with multiple VALUES clauses is:

INSERT INTO table_name (column1, column2, ...)

VALUES (value1, value2, ...),

       (value3, value4, ...),

       ...;


The table_name is the name of the table that you want to insert data into. The column1, column2, ... are the names of the columns that you want to insert data into. The value1, value2, ... are the values that you want to insert into the columns.


For example, the following statement inserts three rows of data into the customers table:

INSERT INTO customers (customer_id, customer_name, customer_email)

VALUES (1, 'John Doe', 'johndoe@example.com'),

       (2, 'Jane Doe', 'janedoe@example.com'),

       (3, 'Peter Smith', 'petersmith@example.com');


You can also use the INSERT INTO statement with multiple VALUES clauses to insert data into a table that has a primary key. In this case, MySQL will automatically assign a unique value to the primary key for each row that is inserted.


For example, the following statement inserts three rows of data into the products table, which has a primary key called product_id:


INSERT INTO products (product_name, product_description, product_price)

VALUES ('T-Shirt', 'A blue t-shirt', 10),

       ('Jeans', 'A pair of blue jeans', 20),

       ('Shoes', 'A pair of black shoes', 30);


MySQL will automatically assign the next available value to the product_id column for each row that is inserted.


For more information on the INSERT INTO statement with multiple VALUES clauses, please see the MySQL documentation.