Latest web development tutorials

SQL INSERT INTO statement

INSERT INTO statement is used to insert a new record to the table.


SQL INSERT INTO statement

INSERT INTO statement is used to insert a new record to the table.

SQL INSERT INTO Syntax

INSERT INTO statement can be written in two forms.

The first form to insert data without specifying the name of the column, just to provide value to be inserted:

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

The second form you need to specify the column names and values ​​to be inserted:

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


The demo database

In this tutorial, we will use w3big sample database.

The following is a selected "Websites" table data:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | 本教程      | http://www.w3big.com/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
+----+--------------+---------------------------+-------+---------+


INSERT INTO examples

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

We can use the following SQL statement:

Examples

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

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


lamp Have you noticed, we do not have to insert any digital id field?
id column is automatically updated in the table of each record has a unique number.


Insert data in the specified column

We can also insert the data in the specified column.

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

Examples

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

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