![]() |
MySQL is an open-source Relational Database Management System that stores data in rows and columns. MySQL is designed to be platform–independent, which means it can run on various operating systems, including Windows, Linux, macOS, and more. MySQL is scalable and can handle databases of varying sizes. It is suitable for small-scale applications as well as large-scale applications. In this article, we will understand How to Insert Multiple Rows with different approaches and so on. Inserting multiple rows in a table reduces the number of queries which is efficient for the memory. Here we will use different methods such as INSERT INTO VALUES, INSERT INTO SELECT, LOAD DATA INFIL, and Batch Inserts. Creating a Table and Inserting Multiple RowsStep 1: Creating a TableWe can create a table by using the “Create Table” Statement. Syntax: CREATE TABLE table_name Example 1: Creating a Table Using “CREATE TABLE” StatementQuery: CREATE TABLE students Method 1: Using INSERT INTO VALUESBy using “INSERT INTO” statement we can insert multiple rows into the table in which we can provide multiple values in the VALUES clause and the values will be inserted. All we have to do is use the INSERT INTO command with our table name and make our desired column, then enter our values according to the columns given. Syntax: INSERT INTO your_table_name (column1, column2, column3) Example 1: Inserting Multiple Rows in MySQLQuery: Let’s insert some data into the students table. INSERT INTO students (StudentID, FirstName, LastName) Output: ![]() Output Explanation: We have inserted records successfully into the students table. Example 2Query: Let’s insert some data into the class table. INSERT INTO class (StudentID, StudentName, Address) Output: ![]() Output Explanation: We have inserted records successfully into the class table. Method 2: Using INSERT INTO SELECTWe can also use the INSERT INTO SELECT statement to insert multiple values from another table, it simply selects our desired values from a table and inserts them into the table we want. Syntax: INSERT INTO destination_table (column1, column2, column3, ...) ExampleQuery: In the given, we will be creating a table first and then we will insert the values, and then we will create our destination in which we want to insert the multiple rows with the existing table. -- Creating first table Output for First Table: ![]() First Table Output for Second Table: ![]() Second Table Explanation: We have successfully fetched the records from the first_table. Method 3: Using LOAD DATA INFILEThis command is used to insert data from a text file to a table in SQL, by using the “LOAD DATA INFILE” command we can easily insert multiple rows into the table. If there is a condition in which we are having a “data.txt” file and a table of employees with columns id, name, salary, and department as Columns. Syntax: LOAD DATA INFILE '/path/to/data.txt' ExampleQuery: In this example, we will create an employees Table then we will load the file along with their path. -- Creating Table Output: ![]() Using LOAD DATA INFILE Explanation: We have inserted records successfully into the employees table. Method 4: Using Batch Inserts for Improved PerformanceUsing Batch Inserts reduces the no. of query, and we can insert multiple rows in a query. Here the syntax used will be “INSERT INTO” and “VALUES” clause. ExampleQuery: Let’s create a table called classroom and then perform Batch Inserts for Improved Performance operations. -- Create the table Output: ![]() Using Batch Inserts Explanation: We have inserted records successfully into the classroom table. ConclusionIn this article, we explored how we can insert multiple rows into a table by using the “INSERT INTO” statement and the “VALUES” clause. For inserting multiple rows first, we have to create a table and then we can easily use the INSERT INTO statement to insert rows into the table, you can move to the steps motioned above to insert multiple rows. FAQs on MySQL Insert Multiple RowsHow do I insert multiple rows in MySQL?
Why should I insert multiple rows at once?
Is there a limit to how many rows I can insert at once?
|
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |