English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SQL Insert Data (INSERT INTO Statement)

In this tutorial, you will learn how to use SQL to insert records into a database table.

Insert data into the table

In the table,InIn the previous chapter,demonstrationA database namedpersonof the table. Now it's time to insert some data into our newly created database table.

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

Syntax

The basic syntax for inserting data into a table can be given as follows:

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

Here,column1,column2, ... etc. represent the names of the table columns, andvalue1,value2, ... etc. represent the corresponding values of these columns.

Let's insertpersonsInsert some records into the table.

Steps1: View table structure

Before adding records, it is best to obtain information about the table structure. Execute the following command on the MySQL command line. It will showPersonThe table displays information about the columns, that is, column names, data types, constraints, etc.

mysql> DESCRIBE persons;

You can use the command to view column information or the structure of any table in MySQL and Oracle databases, while in SQL Server, you can replace the actual table nametable_name. DESCRIBE table_name;EXEC sp_columns table_name;

Steps2: Add records to the table

The following statement inpersonsInsert a new row into the table.

INSERT INTO persons (name, birth_date, phone)
VALUES ('Peter Wilson', '')1990-07-15', '0711-020361);

Did you notice that we did not insert any values for idfield? Because, if you rememberCreate tableIn a chapter, the id field is marked with the AUTO_INCREMENT flag, which tells MySQL that if this field is not specified, it will automatically assign a value to this field.

Note:Non-numeric values (such as strings and dates) must always be enclosed in quotes, while numeric values must never be enclosed in quotes. Additionally, if your string itself contains quotes, you should escape them with a backslash ('Let\'s go').

Similarly, insert another rowpersonsTable, as shown below:

INSERT INTO persons (name, birth_date, phone)
VALUES ('Carrie Simpson', '')1995-05-01', '0251-031259);

In a similar manner,personsInsert another row into the table:

INSERT INTO persons (name, birth_date, phone)
VALUES ('Victoria Ashworth', '1996-10-17', '0695-346721);

Now, if you select fromPersonThe output will be as follows if you select records from the table:

+----+--------------------+------------+-------------+
| id | name               | birth_date | phone       |
+----+--------------------+------------+-------------+
|  1 | Peter Wilson       | 1990-07-15 | 0711-020361 |
|  2 | Carrie Simpson     | 1995-05-01 | 0251-031259 |
|  3 | Victoria Ashworth  | 1996-10-17 | 0695-346721 |
+----+--------------------+------------+-------------+

In the next chapter, we will learn to useSelecting Records from a Table SQL Statements.