Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQL INSERT INTO Statement

SQL INSERT INTO Statement


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


SQL INSERT INTO Statement

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

SQL INSERT INTO Syntax

The INSERT INTO statement can be written in two forms.

The first form does not require specifying the column names for the data to be inserted; you only need to provide the values to be inserted:

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

The second form requires specifying both the column names and the values to be inserted:

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

Parameter Description:


Demonstration Database

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

Below is the data selected from the "Websites" table:


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

INSERT INTO Examples

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

'

)

;

After executing the above SQL and reading the "Websites" table again, the data looks like this:

| lamp | Did you notice that we did not insert any number into the id field?
The id column is automatically updated, and each record in the table has a unique number. | | --- | --- |


Inserting Data into Specified Columns

We can also insert data into specified columns.

The following SQL statement inserts a new row but only inserts data into the "name", "url", and "country" columns (the id field is automatically updated):

Example

INSERT

INTO

Websites

(

name

,

url

,

country

)

VALUES

(

'

stackoverflow

'

,

'

http://stackoverflow.com/

'

,

'

IND

'

)

;

After executing the above SQL and reading the "Websites" table again, the data looks like this: