Basic operation of data in MySQL table-insert data

There are many ways to insert data into the data table, such as inserting data for all columns, inserting data for specified columns, inserting data in batches, etc. The method of inserting data is determined according to different needs.

Insert data for all columns

Under normal circumstances, inserting data into a data table should include all the fields in the table, that is, adding data to all fields in the table. There are two ways to add data to all fields in the table.

1. Specify all field names in the INSERT statement.
You can insert data into the table by listing all the fields in the table using the INSERT statement. The syntax format is as follows:

INSERT INTO 表名(字段名1, 字段名2, ...) VALUES(1,2, ...);

In the above format, field name 1, field name 2, etc. are the field names in the data table, value 1, value 2, etc. are the data that needs to be added to the corresponding field. The order of each value and the type must correspond to the field name.
Create table T1 in the text database and add data to it.
Insert picture description here
Next, insert data through INSERT.
Insert picture description here

2. Do not specify the field name
in the INSERT statement. You can also use the INSERT statement to insert data for all columns without specifying the field name. The syntax format is as follows.

INSERT INTO 表名 VALUES (1,值2...);

Value 1, value 2, etc. indicate the data that needs to be added to each field. The order and type of each value must correspond to the order and type of the fields in the table.
Insert the second piece of data into T1 by INSERT statement without specifying the field name.
Insert picture description here
Insert picture description here

Insert data for the specified column

In some specified scenarios, you may only need to add data for a few fields in the table, and use the default values ​​for other fields. This requires inserting data for the specified column. The syntax format is as follows.

INSERT INTO 表名(字段1,字段2...) VALUES(1,值2...);

In the above format, field name 1, field name 2, etc. represent the field names in the data table, value 1, value 2, etc. represent the data that needs to be added to each field, and the order and type of each value must correspond to the field name.
Use this method to add the third piece of data to T1.
Insert picture description here

Insert data in bulk

In actual development, you will encounter situations where you need to insert multiple records into the database, one by one, which is obviously more troublesome. At this time, batch inserting into the database can improve work efficiency

1. Insert data in
batches for all columns Inserting data in batches is similar to inserting a piece of data. You can list multiple sets of VALUES in the statement. The syntax format is as follows.

INSERT INTO 表名[(字段名1,字段名2...)]
VALUES(1,值2...), (1,值2...), ... (1,值2...);

Create a new data table T2.
Insert picture description here
Insert data into T2 in batches.
Insert picture description here
Insert picture description here
The field name in the SQL statement can be omitted, for example:
Insert picture description here
2. Insert data
in batches for all columns. When inserting data in batches, you can also specify certain columns, and the others are the default values. This is the same as the previous study to insert a piece of data for the specified column similar.
Insert data in batches into the T2 table, and only insert the first two columns.
Insert picture description here

Guess you like

Origin blog.csdn.net/javanofa/article/details/107297182
Recommended