There are two basic syntax for inserting a record:
Insert Basic Syntax One
| category |
Detailed Solution |
| Basic syntax |
Insert into table values (value 1, value 2, value n); |
| Example |
INSERT into user values (2, ' php Chinese web ', ' male ') |
| Example description |
Insert a value ID of 2 to the user table, name Li Wenkei, sex is male |
Insert Basic Syntax Two
| category |
Detailed Solution |
| Basic syntax |
Insert into table (field 1, Field 2, field N) VALUES (value 1, value 2, value n); |
| Example |
INSERT into User (Id,username,sex) values (213, ' Little Shenyang ', 1); |
| Example description |
Insert ID 213,username to user table for small Shenyang, sex is 1 |
Description
The difference between basic Syntax 1 and basic Syntax 2 is:
- The INSERT statement for Basic Syntax 1, the number of fields in the table that must be inserted in the number of values. One cannot be more, nor one can be less. If there is a default value, do not want to pass, you can write null.
- Basic Syntax 2, unless a required field must be written to a value. If you do not want to write with a default value, you can ignore the write. MySQL will automatically fill in the default values.
- In basic Syntax 2, values are ordered in the same order as the user (Id,username,sex) field.
Given that there is a table for the user table, we describe the fields, field descriptions, types and fields, and the required states, with the following table structure:
| Field |
ID |
username |
Email |
Password |
Sex |
| Chinese description |
Number |
User name |
Mailbox |
Password |
Gender |
| Type description |
Int |
varchar (50) |
varchar (60) |
varchar (32) |
tinyint |
| Default Value Description |
Self-increment |
Must fill in |
Optional field, the default value is [email protected]n |
Optional fields |
Required Fields |
According to
Basic Grammar OneWrite the INSERT statement in the table:
INSERT into user values (null, ' PHP Chinese web ', ' [email protected] ', NULL, 1);
Attention
- You can not specify a field name, but the order after values should be the same as the table field.
- A field with a default value can not be written, or it is the default value.
- You can write null if there is a default value or if a nullable field does not want to pass in a specific value.
- The data format must be consistent with the data format specified by the table.
According to
Basic Syntax TwoWrite the INSERT statement in the table:
INSERT into User (Username,sex) VALUES (' PHP Chinese web ', 1);
Attention
- Self-increment IDs can be used without passing in values, and the value of this field is automatically increased by 1 each time it is inserted.
- Fields that have default values and can be empty do not pass
- The Insertion Order of table user (Username,sex) is whichever
- Basic syntax two for more common usage
Basic syntax morphing: inserting multiple records at once
INSERT into User (username,password,sex)
Values (' Huang Xiaoming ', ' abcdef ', 1),
(' Angelababy ', ' Bcdeef ', 0),
(' Chenhe ', ' 123456 ', 1),
(' Wangbaoqiang ', ' 987654 ', 1);
MySQL Add and delete changes to the "increase"