Tutorials  /  Databases

How to INSERT Multiple Records in SQL: Methods & Examples

Ccentron Redaktion · December 2024 ·3 min read ·Databases, Tutorial

Need of SQL Insert INTO Multiple Rows Query

SQL INSERT query inserts data into the columns of a particular table.

The normal SQL INSERT query inputs the data values in a single row. In case when we want to insert data in multiple rows at once, this query fails.

Thus, in order to save the execution time, we need to use the SQL INSERT query in such a manner that it injects data into multiple rows at once.

Having understood the need of SQL Insert query for multiple rows, let us get started with the implementation of the same.

DB

Matching infrastructure at centron

Databases need care in production: centron can take over updates, backups and monitoring – from a single instance to a full cluster. Explore managed clusters →

Traditional SQL INSERT Query to Insert Multiple Records

Traditional SQL INSERT query injects input data into multiple rows. In this technique, we need to the insert query as many times we want to input data into the rows of the table.

The basic drawback of this query is the overhead of execution of every insert query for multiple rows injection.

Example:

SQL
create table Info(id integer, Cost integer, city varchar(200));
insert into Info(id, Cost,city) values(1, 100,"Pune");
insert into Info(id, Cost,city) values(2, 50, "Satara");
insert into Info(id, Cost,city) values(3, 65,"Pune");
insert into Info(id, Cost,city) values(4, 97,"Mumbai");
insert into Info(id, Cost,city) values(5, 12,"USA");
select * from Info;

Output:

1 100 Pune

2 50 Satara

3 65 Pune

4 97 Mumbai

5 12 USA

INSERT-SELECT-UNION Query to Insert Multiple Records

In the above section, we got to know that INSERT INTO query injects multiple records. But, if we observe the output of it, we get to know that the clause ‘INSERT INTO’ is repeated many times.

Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table.

The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.

SQL
create table Info(id integer, Cost integer);
INSERT INTO Info (id, Cost)  
SELECT 1, '123'  
UNION ALL   
SELECT 2, '234'  
UNION ALL  
SELECT 3, '456';  

select * from Info;

Output:

1 123

2 234

3 456

Row Construction to Insert Multiple Records

The SQL INSERT query is used in a manner wherein we make use of a single INSERT query to insert multiple records within a single point of execution.

Syntax:

SQL
INSERT INTO Table (columns)  
VALUES (val1, val2, valN); 

Example:

SQL
create table Info(id integer, Cost integer,city nvarchar(200));
INSERT INTO Info (id,Cost,city)  
VALUES (1,200, 'Pune'), (2, 150,'USA'), (3,345, 'France');  

select * from Info;

Output:

1 200 Pune

2 150 USA

3 345 France

Conclusion

By this we have come to the end of this topic. Herein we have covered three different techniques to INSERT data values across multiple records of a table.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Databases
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren