WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

SQL INSERT INTO

INSERT INTO statement is used to insert new records into the table.


SQL INSERT INTO statement

INSERT INTO statement is used to insert new records into the table.

SQL INSERT INTO syntax

INSERT INTO statements can be written in two forms.

The first form does not need to specify the name of the column to insert data, only the value to be inserted:

INSERT INTO table_name
VALUES (value1,value2,value3,...);

The second form requires the column name and the value to be inserted:

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);


Demo database

In this tutorial, we will use the welookups sample database.

Here is the data from the "Websites" table:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.com/ | 1     | USA     |
| 2  | Indiatimes          | https://www.Indiatimes.com/| 13    | IN      |
| 3  | zomato      | http://www.zomato.com/ | 4689  | 88      |
| 4  | indiamart          | http://indiamart.com/      | 20    | IN      |
| 5  | Facebook     | https://www.facebook.com/| 3     | USA     |
+----+--------------+---------------------------+-------+---------+


INSERT INTO Example

Suppose we want to insert a new row into the "Websites" table.

We can use the following SQL statement:


Example

INSERT INTO Websites (name, url, alexa, country) VALUES ('Baidu','https://www.baidu.com/','4','CN');

Execute the above SQL, and then read the "Websites" table, the data is as follows:


lamp Did you notice that we didn't insert any numbers into the id field?
The id column is automatically updated, and each record in the table has a unique number.


Insert data in the specified column

We can also insert data in specified columns.

The following SQL statement will insert a new row, but insert data only in the "name", "url", and "country" columns (the id field will be updated automatically):

Example

INSERT INTO Websites (name, url, country) VALUES ('stackoverflow', 'http://stackoverflow.com/', 'IND');

Execute the above SQL, and then read the "Websites" table, the data is as follows: