Curriculum
Course: MYSQL
Login

Curriculum

MYSQL

MySQL References

0/140
Text lesson

MySQL INSERT INTO

The MySQL INSERT INTO Statement

The INSERT INTO statement is utilized to add new records to a table.

INSERT INTO Syntax

You can write the INSERT INTO statement in two ways:

  1. Specify both the column names and the corresponding values to be inserted:
    INSERT INTO table_name (column1, column2, column3, …)
    VALUES (value1, value2, value3, …);
  2. When inserting values into all columns of a table, you don’t need to explicitly list the column names in the SQL query. Ensure that the values are provided in the same order as the columns in the table. Here is the INSERT INTO syntax for this scenario:
    INSERT INTO table_name
    VALUES (value1, value2, value3, …);

Demo Database

Here is an excerpt from the “Customers” table in the Northwind sample database:

CustomerID

CustomerName

ContactName

Address

City

PostalCode

Country

89

White Clover Markets

Karl Jablonski

305 – 14th Ave. S. Suite 3B

Seattle

98128

USA

90

Wilman Kala

Matti Karttunen

Keskuskatu 45

Helsinki

21240

Finland

91

Wolski

Zbyszek

ul. Filtrowa 68

Walla

01-012

Poland

INSERT INTO Example

The following SQL statement adds a new record to the “Customers” table:

Example

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES (‘Cardinal’‘Tom B. Erichsen’‘Skagen 21’‘Stavanger’‘4006’‘Norway’);

The “Customers” table selection will now appear as follows:

CustomerID

CustomerName

ContactName

Address

City

PostalCode

Country

89

White Clover Markets

Karl Jablonski

305 – 14th Ave. S. Suite 3B

Seattle

98128

USA

90

Wilman Kala

Matti Karttunen

Keskuskatu 45

Helsinki

21240

Finland

91

Wolski

Zbyszek

ul. Filtrowa 68

Walla

01-012

Poland

92

Cardinal

Tom B. Erichsen

Skagen 21

Stavanger

4006

Norway

Did you notice that we did not insert any number into the CustomerID field?

The CustomerID column is automatically generated as an auto-increment field when inserting a new record into the table.

Insert Data Only in Specified Columns

You can also insert data into specific columns.

The following SQL statement inserts a new record, providing data only for the “CustomerName”, “City”, and “Country” columns (CustomerID will be generated automatically):

Example

INSERT INTO Customers (CustomerName, City, Country)
VALUES (‘Cardinal’‘Stavanger’‘Norway’);

The “Customers” table selection will now appear as follows:

CustomerID

CustomerName

ContactName

Address

City

PostalCode

Country

89

White Clover Markets

Karl Jablonski

305 – 14th Ave. S. Suite 3B

Seattle

98128

USA

90

Wilman Kala

Matti Karttunen

Keskuskatu 45

Helsinki

21240

Finland

91

Wolski

Zbyszek

ul. Filtrowa 68

Walla

01-012

Poland

92

Cardinal

null

null

Stavanger

null

Norway