A C C R E T E

How to Add Multiple Records in SQL ?

In SQL, inserting multiple rows into a table is a common operation when dealing with large datasets.

The process is relatively simple, but it’s essential to understand how to do it efficiently to save time and reduce errors.

In this guide, we’ll explore the different methods of inserting multiple rows in SQL and how to choose the most appropriate one for your specific use case.

Using the INSERT INTO Statement

The most straightforward method of inserting multiple rows in SQL is using the INSERT INTO statement.

This statement inserts one or more rows into a table. Here’s the basic syntax:


INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...),
(value1, value2, value3, ...),
(value1, value2, value3, ...), ...

The values in each row should be enclosed in parentheses and separated by commas.

You can insert multiple rows at once by separating each set of values with a comma.

For example, suppose we have a table named “employees” with columns “id”, “name”, “email”, and “salary”.

We can insert three rows into this table using the following query:


INSERT INTO employees (name, email, salary)
VALUES ('John Smith', 'john.smith@example.com', 50000),
('Jane Doe', 'jane.doe@example.com', 60000),
('Bob Johnson', 'bob.johnson@example.com', 55000);

This query inserts three rows into the “employees” table with the specified names, email addresses, and salaries.

Using the UNION Operator

The UNION operator allows you to combine the results of two or more SELECT statements into a single result set.

You can use this operator to insert multiple rows into a table. Here’s an example:


INSERT INTO table_name (column1, column2, column3, ...)
SELECT value1, value2, value3, ...
UNION
SELECT value1, value2, value3, ...
UNION
SELECT value1, value2, value3, ...

In this example, we’re selecting the values for each row using the SELECT statement and then combining them with the UNION operator.

You can add as many SELECT statements as you need, and each SELECT statement should return the same number of columns.

For instance, suppose we have a table named “products” with columns “id”, “name”, and “price”.

We can insert three rows into this table using the following query:


INSERT INTO products (name, price)
SELECT 'Laptop', 1000
 UNION
SELECT 'Smartphone', 500
UNION
SELECT 'Tablet', 800;

This query inserts three rows into the “products” table with the specified names and prices.

Using the VALUES Row Constructor

Another method of inserting multiple rows in SQL is using the VALUES row constructor.

This method is supported in SQL Server and PostgreSQL. Here’s an example:


INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3), (value1, value2, value3), (value1, value2, value3), ...

In this example, we’re using the VALUES row constructor to insert multiple rows into the table in a single query.

The rows are enclosed in parentheses and separated by commas.

For example, suppose we have a table named “students” with columns “id”, “name”, “age”, and “grade”.

We can insert three rows into this table using the following query:


INSERT INTO students (name, age, grade)
VALUES ('John Doe', 20, 'A'),
('Jane Smith', 22, 'B'),
('Bob Johnson', 19, 'C');

This query inserts three rows into the “students” table with the specified names, ages, and grades.

Using Prepared Statements

Prepared statements can also be used to insert multiple rows into a table.

Prepared statements are SQL statements that are precompiled and stored in a database.

They can be executed multiple times with different parameter values.

Here’s an example of how to use prepared statements to insert multiple rows into a table:


PREPARE insert_statement (text, text, numeric) AS
INSERT INTO table_name (column1, column2, column3) VALUES ($1, $2, $3); EXECUTE insert_statement ('John Smith', 'john.smith@example.com', 50000); EXECUTE insert_statement ('Jane Doe', 'jane.doe@example.com', 60000); EXECUTE insert_statement ('Bob Johnson', 'bob.johnson@example.com', 55000);

In this example, we’re using prepared statements to insert three rows into the “table_name” table with different parameter values for each execution.

The values are specified using placeholders ($1, $2, $3) that are later replaced by the actual values during execution.

Using the COPY Command

The COPY command is a PostgreSQL-specific feature that allows you to copy data between files and tables. This command can also be used to insert multiple rows into a table.

Here’s an example of how to use the COPY command to insert multiple rows into a table:


COPY table_name (column1, column2, column3, ...)
FROM 'file.csv'
DELIMITER ','
CSV HEADER;

In this example, we’re copying data from a CSV file into the “table_name” table.

The file should contain the values for each row, separated by commas.

The delimiter option specifies the delimiter used in the file, and the CSV option specifies that the file is in CSV format.

The HEADER option specifies that the first line of the file contains the column names.

Conclusion

In conclusion, inserting multiple rows into a table is a common operation in SQL that can be done using various methods.

The choice of method depends on the specific use case and the database system being used.

The INSERT INTO statement is the most straightforward method, while the other methods offer different advantages such as speed and flexibility.

By using the appropriate method for your use case, you can efficiently manage large datasets and avoid errors in your database.

Moreover, this blog post is fabricated by the content experts at Accrete Infosolution Technologies LLP, a reliable web development service provider that has years of expertise in providing IT services across the globe. Contact us today to hire web developers for your dream project!

You Might Also Like:
What is Database Sharding?
10 Must-Know Python String Functions
What are the Elements, Tags, and Attributes in HTML?
Top 30 Linux Commands You Must Know
The Importance of CSS in Web Development

Find an agent now

Telephone

+91 079 232 13063

Time Schedule

Office Time

Mon - Fri: 9:00 - 18:00