MySQL database - MySQL INSERT: insert data (add data)

After the database and tables are successfully created, data needs to be inserted into the tables of the database. In MySQL, you can use the INSERT statement to insert one or more rows of tuple data into an existing table in the database.

basic grammar

The INSERT statement has two grammatical forms, the INSERT...VALUES statement and the INSERT...SET statement.

1) INSERT...VALUES statement

The syntax for INSERT VALUES is:

INSERT INTO <表名> [ <列名1> [ , … <列名n>] ]
VALUES (值1) [… , (值n) ];

The syntax is explained as follows:

  • <表名>: Specifies the name of the table to be operated.
  • <列名>: Specify the name of the column to insert data into. If you want to insert data into all the columns in the table, all the column names can be omitted, just use INSERT<table name>VALUES(…) directly.
  • VALUES OR  VALUE clause: This clause contains the list of data to be inserted. The order of the data in the data list should correspond to the order of the columns.

2) INSERT...SET statement

The syntax format is:

INSERT INTO <表名>
SET <列名1> = <值1>,
        <列名2> = <值2>,

This statement is used to directly specify the corresponding column values ​​for certain columns in the table, that is, the column name of the data to be inserted is specified in the SET clause, col_name is the specified column name, and the equal sign is followed by the specified data, while for For unspecified columns, the column value will be specified as the default value of the column.

It can be seen from the two forms of the INSERT statement:

  • Use the INSERT...VALUES statement to insert a row of data into the table, or insert multiple rows of data;
  • Use the INSERT...SET statement to specify the value of each column in the inserted row, or specify the value of some columns;
  • The INSERT...SELECT statement inserts data from other tables into a table.
  • Use the INSERT...SET statement to insert the values ​​of some columns into the table, which is more flexible;
  • The INSERT...VALUES statement can insert multiple pieces of data at one time.

In MySQL, processing multiple inserts with a single INSERT statement is faster than using multiple INSERT statements.

When using a single INSERT statement to insert multiple rows of data, you only need to enclose each row of data in parentheses.

Add values ​​to all fields in the table

Create a course information table tb_courses in the test_db database, including course number course_id, course name course_name, course credit course_grade and course notes course_info, the entered SQL statement and execution results are as follows:

mysql> CREATE TABLE tb_courses
    -> (
    -> course_id INT NOT NULL AUTO_INCREMENT,
    -> course_name CHAR(40) NOT NULL,
    -> course_grade FLOAT NOT NULL,
    -> course_info CHAR(100) NULL,
    -> PRIMARY KEY(course_id)
    -> );
Query OK, 0 rows affected (0.00 sec)

There are two ways to insert values ​​into all fields in a table: one is to specify all field names; the other is to not specify field names at all.

【Example 1】 Insert a new record in the tb_courses table, the value of course_id is 1, the value of course_name is "Network", the value of course_grade is 3, and the value of info is "Computer Network".

Before executing the insert operation, view the SQL statement and execution results of the tb_courses table as follows:

mysql> SELECT * FROM tb_courses;
Empty set (0.00 sec)

The query result shows that the content of the current table is empty and there is no data. Next, the operation of inserting data is performed. The entered SQL statement and execution process are as follows:

mysql> INSERT INTO tb_courses
    -> (course_id,course_name,course_grade,course_info)
    -> VALUES(1,'Network',3,'Computer Network');
Query OK, 1 rows affected (0.08 sec)
mysql> SELECT * FROM tb_courses;
+-----------+-------------+--------------+------------------+
| course_id | course_name | course_grade | course_info      |
+-----------+-------------+--------------+------------------+
|         1 | Network     |            3 | Computer Network |
+-----------+-------------+--------------+------------------+
1 row in set (0.00 sec)

You can see that the record was inserted successfully. When inserting data, all fields of the tb_courses table are specified, so new values ​​will be inserted for each field.

The order of the column names behind the INSERT statement may not be the order in which the tb_courses table was defined, that is, when inserting data, it does not need to be inserted in the order defined in the table, as long as the order of the values ​​is the same as that of the column fields.

【Example 2】 Insert a new record in the tb_courses table, the value of course_id is 2, the value of course_name is "Database", the value of course_grade is 3, and the value of info is "MySQL". The entered SQL statement and execution result are as follows:

mysql> INSERT INTO tb_courses
    -> (course_name,course_info,course_id,course_grade)
    -> VALUES('Database','MySQL',2,3);
Query OK, 1 rows affected (0.08 sec)
mysql> SELECT * FROM tb_courses;
+-----------+-------------+--------------+------------------+
| course_id | course_name | course_grade | course_info      |
+-----------+-------------+--------------+------------------+
|         1 | Network     |            3 | Computer Network |
|         2 | Database    |            3 | MySQL            |
+-----------+-------------+--------------+------------------+
2 rows in set (0.00 sec)

When using INSERT to insert data, the column name list column_list is allowed to be empty. At this time, the value list needs to specify a value for each field of the table, and the order of the values ​​must be the same as the order of the field definition in the data table.

【Example 3】 Insert a new record in the tb_courses table, the value of course_id is 3, the value of course_name is "Java", the value of course_grade is 4, and the value of info is "Jave EE". The entered SQL statement and execution result are as follows:

mysql> INSERT INTO tb_courses
    -> VLAUES(3,'Java',4,'Java EE');
Query OK, 1 rows affected (0.08 sec)
mysql> SELECT * FROM tb_courses;
+-----------+-------------+--------------+------------------+
| course_id | course_name | course_grade | course_info      |
+-----------+-------------+--------------+------------------+
|         1 | Network     |            3 | Computer Network |
|         2 | Database    |            3 | MySQL            |
|         3 | Java        |            4 | Java EE          |
+-----------+-------------+--------------+------------------+
3 rows in set (0.00 sec)

There is no insertion list specified in the INSERT statement, only a list of values. In this case, the list of values ​​specifies the inserted values ​​for each field column, and the values ​​must be in the same order as the field definitions in the tb_courses table.

Note: Although the column name of the inserted data can be ignored when using INSERT to insert data, if the value does not contain the column name, the value after the VALUES keyword must not only be complete, but also must be in the same order as the column in the table definition. If the structure of the table is modified, adding, deleting or changing the position of the column, these operations will make the order of inserting data in this way also change at the same time. If you specify column names, you will not be affected by changes in the table structure.

Add a value to the specified field in the table

To insert data for a specified field of a table is to only insert values ​​into some fields in the INSERT statement, while the values ​​of other fields are the default values ​​when the table is defined.

[Example 4] Insert a new record in the tb_courses table, the value of course_name is "System", the value of course_grade is 3, and the value of course_info is "Operating System". The entered SQL statement and execution result are as follows:

mysql> INSERT INTO tb_courses
    -> (course_name,course_grade,course_info)
    -> VALUES('System',3,'Operation System');
Query OK, 1 rows affected (0.08 sec)
mysql> SELECT * FROM tb_courses;
+-----------+-------------+--------------+------------------+
| course_id | course_name | course_grade | course_info      |
+-----------+-------------+--------------+------------------+
|         1 | Network     |            3 | Computer Network |
|         2 | Database    |            3 | MySQL            |
|         3 | Java        |            4 | Java EE          |
|         4 | System      |            3 | Operating System |
+-----------+-------------+--------------+------------------+
4 rows in set (0.00 sec)

You can see that the record was inserted successfully. As the query results show, the course_id field here is automatically added with an integer value of 4. At this time, the course_id field is the primary key of the table and cannot be empty. The system automatically inserts an auto-incremented sequence value for this field. When inserting records, if some fields do not specify the insertion value, MySQL will insert the default value when the field is defined.

Copy table data using the INSERT INTO…FROM statement

The INSERT INTO...SELECT...FROM statement is used to quickly retrieve data from one or more tables and insert the data into another table as row data.

The SELECT clause returns a queried result set, and the INSERT statement inserts the result set into the specified table. The number of fields and the data types of each row of data in the result set must be completely consistent with the table being operated.

Create a data table tb_courses_new with the same structure as the tb_courses table in the database test_db. The SQL statement and execution process for creating the table are as follows:

mysql> CREATE TABLE tb_courses_new
    -> (
    -> course_id INT NOT NULL AUTO_INCREMENT,
    -> course_name CHAR(40) NOT NULL,
    -> course_grade FLOAT NOT NULL,
    -> course_info CHAR(100) NULL,
    -> PRIMARY KEY(course_id)
    -> );
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM tb_courses_new;
Empty set (0.00 sec)

[Example 5] Query all records from the tb_courses table and insert them into the tb_courses_new table. The entered SQL statement and execution result are as follows:

mysql> INSERT INTO tb_courses_new
    -> (course_id,course_name,course_grade,course_info)
    -> SELECT course_id,course_name,course_grade,course_info
    -> FROM tb_courses;
Query OK, 4 rows affected (0.17 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> SELECT * FROM tb_courses_new;
+-----------+-------------+--------------+------------------+
| course_id | course_name | course_grade | course_info      |
+-----------+-------------+--------------+------------------+
|         1 | Network     |            3 | Computer Network |
|         2 | Database    |            3 | MySQL            |
|         3 | Java        |            4 | Java EE          |
|         4 | System      |            3 | Operating System |
+-----------+-------------+--------------+------------------+
4 rows in set (0.00 sec)

Dark horse programmer MySQL database entry to proficiency, from mysql installation to mysql advanced, mysql optimization all covered

Guess you like

Origin blog.csdn.net/Itmastergo/article/details/130400167