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:
VALUES (value1,value2,value3,...);
The second form requires specifying the column name and Inserted value:
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 ExampleSuppose we want to insert a new row into the "Websites" table. We can use the following SQL statement:
Execute the above SQL and then read the "Websites" table. The data is as follows:
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 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
VALUES ('stackoverflow', 'http://stackoverflow.com/', 'IND');
Execute the above SQL, then read the "Websites" table, the data is as follows: