The INSERT INTO statement is utilized to add new records to a table.
You can write the INSERT INTO statement in two ways:
INSERT INTO table_name (column1, column2, column3, …) VALUES (value1, value2, value3, …); |
INSERT INTO table_name VALUES (value1, value2, value3, …); |
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 |
The following SQL statement adds a new record to the “Customers” table:
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. |
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):
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 |