🚀 Think you’ve got what it takes for a career in Data? Find out in just one minute!

INSERT INTO SQL: Insert data with this SQL query

-
3
 m de lecture
-
INSERT INTO: Insert data with this SQL query

To insert data into your SQL table, you can use the INSERT INTOSQL query. But how does it work? That's what we're going to look at in this article.

What is an INSERT INTO SQL request?

INSERT INTO SQL is one of the most commonly used commands in the SQL language. And with good reason: this query allows you to integrate new records into your database. This can be one or more rows, depending on your needs.

Good to know: INSERT INTO SQL is the command to use for all database management systems. Whether you’re using Oracle, Mysql, Transact-SQL, etc.

How do I use the SQL query INSERT INTO?

INSERT INTO SQL can be used to insert one or more rows into your table. But it’s also possible to use this command in more complex queries. In this article, we’ll take a look at some of these applications.

Insert a line

There are two ways of doing this.

You can insert a line by specifying all the columns. In this case, the syntax is as follows:

INSERT INTO table VALUES (‘value 1’, ‘value 2’, …)

If you choose this option, be sure to respect the order of the columns. The database management system interprets SQL queries according to the information you give it. So if you don’t need to record new values for certain columns, you’ll need to specify “NULL” (instead of value 1,2…).

The advantage is that you don’t need to write the name of each limit. This greatly reduces typing errors.

You can specify only those columns for which you wish to integrate new data. To do this, use the following syntax:

INSERT INTO table (column-name_1, column-name_2, …)
VALUES (‘value 1’, ‘value 2’, …)

Here, column order is no longer as important. Be careful, however, with the order of values. They must correspond to the columns to which they are assigned.

Insert several lines

It is often necessary to insert several lines to enrich your table.

To help you understand, here’s a concrete example of a Customer file to which you want to add the contact details of several customers.

Here’s the query:

INSERT INTO customer (first name, last name, city, email)
VALUES
(‘Justine’, ‘Martin’, ‘Paris’, ‘justinemartin@gmail.com’ ),
(‘Thomas’, ‘Durant’, ‘Bordeaux’, ‘tomtom@sfr.fr’),
(‘Marie’, ‘Leroy’, ‘Angers’, ‘marieleroy@laposte.net’),
(‘Vanessa’, ‘Savary’, ‘Marseille’, ‘vanessa13@gmail.com’)

Use the INSERT INTO command to integrate the following data into your table.

First Name Last Name City Email
Justin Martin Paris justinemartin@gmail.com
Thomas Durant Bordeaux tomtom@sfr.fr
Marie Leroy Angers marieleroy@laposte.net
Vanessa Savary Marseille vanessa13@gmail.com

Copy data from another table

By combining several queries, you can also copy data from an A array to a table.

To do this, use the following syntax:

INSERT INTO my_table_1 SELECT column_1,column_2,column_2 FROM my_table_2 WHERE conditions

Let’s take an example:

You have two tables. The first corresponds to a “customer” file:

Prénom Nom Ville Email
Justin Martin Paris justinemartin@gmail.com
Thomas Durant Bordeaux tomtom@sfr.fr
Marie Leroy Angers marieleroy@laposte.net
Vanessa Savary Marseille vanessa13@gmail.com

And the second, to a “Prospecting” file:

First Name Last Name City Email
Justin Martin Paris justinemartin@gmail.com
Thomas Durant Bordeaux tomtom@sfr.fr
Marie Leroy Angers marieleroy@laposte.net
Vanessa Savary Marseille vanessa13@gmail.com

Having converted a prospect (Jean Bernard), you want to transfer him to your customer file.

Here’s the query:

INSERT INTO Customer SELECT (first name, last name, city, email) FROM prospect WHERE id = 1

And the result:

First Name Last Name City Email
Justin Martin Paris justinemartin@gmail.com
Thomas Durant Bordeaux tomtom@sfr.fr
Marie Leroy Angers marieleroy@laposte.net
Vanessa Savary Marseille vanessa13@gmail.com
Jean Bernard Valenciennes jbernard@gmail.com

Develop your SQL skills with DataScientest

Manipulating relational data doesn’t just mean adding new information. It’s also possible to delete, modify, update and so on.

For all these actions, there are a multitude of SQL queries. If you want to know them all, data training is more than necessary. That’s why we’ve developed DataScientest.

We offer a range of industry-specific training courses to help you develop your skills.

Facebook
Twitter
LinkedIn

DataScientest News

Sign up for our Newsletter to receive our guides, tutorials, events, and the latest news directly in your inbox.

You are not available?

Leave us your e-mail, so that we can send you your new articles when they are published!

Related articles

icon newsletter

DataNews

Get monthly insider insights from experts directly in your mailbox