SQL Getting Sta...login
SQL Getting Started Tutorial Manual
author:php.cn  update time:2022-04-12 14:15:40

SQL INSERT INTO



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


SQL INSERT INTO statement

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

SQL INSERT INTO syntax

The INSERT INTO statement can be written in two forms.

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

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

The second form requires specifying the column name and Inserted value:

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


Demo Database

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

The following is the data selected from the "Websites" table:

+----+--------------+--- ------------------------+------+---------+
| id | name | url                                                                    --------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | Taobao | https://www.taobao.com/ | 13 | CN |
| 3 | php Chinese website | http://www.php.cn/ | 4689 | CN |
| 4 | Weibo | http://weibo.com/ | 20 | CN |
| 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:


lampDid 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 the specified column.

The following SQL statement will insert a new row, but only insert data 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, then read the "Websites" table, the data is as follows: