In MySQL, the INSERT statement is used to insert new data rows into the database table. The basic syntax of the INSERT statement is as follows:
INSERT INTO table_name (column1, column2, column3, …)
VALUES (value1, value2, value3, …);
Among them:
- table_name: The name of the table into which data is to be inserted.
- column1, column2, column3, …: The column names to insert data into.
- value1, value2, value3, …: Data values to be inserted.
The following is an example:
Suppose there is a table named students, containing columns id, name and age. You want to insert a new piece of data into this table, with id 1, name "Zhang San", and age 20.
INSERT INTO students (id, name, age)
VALUES (1, 'Zhang San', 20);
After executing the above INSERT statement, the data will be inserted to the students table.
Note:
If the inserted value is of string type, it needs to be enclosed in single quotes or double quotes.
- If the data columns to be inserted are not all columns, you can omit the columns that do not require data to be inserted, but make sure that the order of the inserted values corresponds to the order of the columns.
- If the inserted value is of string type, it needs to be enclosed in single quotes or double quotes.
- If the inserted value is a date type or time type, you need to use the appropriate date or time format.
- If the inserted value is NULL, you can use the NULL keyword directly.
- If the inserted data violates the constraints of the table (such as primary key or unique constraints), an error will occur and the insertion will fail.
- If you want to insert multiple pieces of data, you can use the INSERT INTO... VALUES statement to execute it multiple times, or use the INSERT INTO... SELECT statement to select data from other tables for insertion.
The above is the detailed content of How to use insert statement in mysql. For more information, please follow other related articles on the PHP Chinese website!