Tags
Asked 3 years ago
17 Jun 2021
Views 254
Alysha

Alysha posted

insert multiple rows at once in a MySQL database

insert multiple rows at once in a MySQL database
shabi

shabi
answered Apr 28 '23 00:00

To insert multiple rows at once in a MySQL database, you can use the INSERT INTO statement with multiple value sets enclosed in parentheses. Here's an example:



INSERT INTO mytable (column1, column2, column3) VALUES
('value1', 'value2', 'value3'),
('value4', 'value5', 'value6'),
('value7', 'value8', 'value9');

In this example, mytable is the name of the table, and column1, column2, and column3 are the names of the columns that the values will be inserted into. The VALUES keyword is followed by multiple value sets enclosed in parentheses, separated by commas. Each value set corresponds to a row that will be inserted into the table.

Make sure that the number of values in each value set matches the number of columns in the INSERT INTO statement, and that the data types of the values match the data types of the corresponding columns in the table.

Using this method to insert multiple rows at once can be more efficient than executing multiple INSERT INTO statements for each row separately. However, keep in mind that the maximum number of rows that your database server can handle in a single statement may vary depending on your server's configuration, so it's important to avoid exceeding this limit.
Post Answer