The SQL INSERT INTO statement is used to insert new records in a table.
INSERT INTO
There are two possible ways to insert data into the table using The INSERT INTO statement.
INSERT INTO table_name (column_1, column_2, column_3) VALUES (value_1, value_2, value_3);
INSERT INTO table_name VALUES (value_1, value_2, value_3, value_4);
Consider a table with some default data.
The following SQL statement will insert a new record in the “customers” table:
INSERT INTO customers (name, address, city, zipCode, country) VALUES ('Cardinal', 'Skagen 21', 'Stavanger', '4006', 'Norway'); -- 1 record inserted. -- +----------------------------------------------------------------------------------------------------+ -- | customerID | name | address | city | zipCode | country | -- |----------------------------------------------------------------------------------------------------| -- | 89 | White Clover Markets | 305 - 14th Ave. S. Suite 3B | Seattle | 98128 | USA | -- | 90 | Wilman Kala | Keskuskatu 45 | Helsinki | 21240 | Finland | -- | 92 | Cardinal | Skagen 21 | Stavanger | 4006 | Norway | -- +----------------------------------------------------------------------------------------------------+
Inserting Data Only in Specified Columns It is also possible to only insert data in specific defined columns in insert query. The following SQL statement will insert a new record, but only insert data in the “name”, “city”, and “country” columns and customerID will be updated automatically:
INSERT INTO customers (name, city, country) VALUES ('Cardinal', 'Stavanger', 'Norway'); -- 1 record inserted. -- +----------------------------------------------------------------------------------------------------+ -- | customerID | name | address | city | zipCode | country | -- |----------------------------------------------------------------------------------------------------| -- | 89 | White Clover Markets | 305 - 14th Ave. S. Suite 3B | Seattle | 98128 | USA | -- | 90 | Wilman Kala | Keskuskatu 45 | Helsinki | 21240 | Finland | -- | 92 | Cardinal | null | Stavanger | null | Norway | -- +----------------------------------------------------------------------------------------------------+