The INSERT INTO statement is used to insert a new record into the table.
SQL INSERT INTO statement
The INSERT INTO statement is used to insert a new record into the table.
SQL INSERT INTO syntax
INSERT INTO statements can have two forms of writing.
The first form does not need to specify the column name of the data to be inserted, just provide the inserted value:
INSERT INTO table_name
VALUES (value1,value2,value3,...);
VALUES (value1,value2,value3,...);
The second form needs to specify the column name and the inserted value:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
VALUES (value1,value2,value3,...);
Demo database
In this tutorial, we will use the RUNOOB sample database.
The following is the data selected from the "Websites" table:
+ ---- + -------------- + --------------------------- + - ------ + --------- + | id | name | url | alexa | country | + ---- + -------------- + - -------------------------- + ------- + --------- + | 1 | Google | Https : //www.google.cm/ | 1 | USA | | 2 | Taobao | https : //www.taobao.com/ | 13 | CN | | 3 | Rookie Tutorial | http : //www.runoob.COM / | 4689 | CN | | 4 | Twitter | HTTP : //weibo.com/ | 20 | CN | | 5 | Facebook | HTTPS : //www.facebook.com/ | 3 | USA | + --- - + -------------- + --------------------------- + ----- - + --------- +
INSERT INTO instance
Suppose we want to insert a new line into the "Websites" table.
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, and then read the "Websites" table, the data is as follows:
Did you notice that we did not insert any numbers into the id field? The id column is automatically updated, and each record in the table has a unique number. |
Inserts data in the specified column
We can also insert data in the specified column.
The following SQL statement inserts a new line, but only the data is inserted in the "name", "url", and "country" columns (the id field is automatically updated):
Examples
The INSERT the INTO Websites ( name , URL , Country ) the VALUES ( ' StackOverflow ' , ' http://stackoverflow.com/ ' , ' IND ' ) ;
Execute the above SQL, and then read the "Websites" table, the data is as follows: