How to implement the statement of inserting data in MySQL?
When using a MySQL database, inserting data is a very basic and common operation. By inserting data, new records can be added to database tables to provide support for business operations. This article will introduce how to use the INSERT statement in MySQL to implement data insertion operations, and provide specific code examples.
The INSERT statement in MySQL is used to insert new records into the database table. The basic syntax format is as follows:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Among them, table_name is the name of the target table, column1, column2, etc. are the names of the fields to be inserted, and value1, value2, etc. are the specific values or strings to be inserted.
Below, we use a specific example to demonstrate how to use the INSERT statement to insert data.
Suppose there is a table named students with the following structure:
CREATE TABLE students ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, age INT NOT NULL, gender ENUM('male', 'female') NOT NULL );
Now, we want to insert a new record into the students table. You can use the following INSERT statement:
INSERT INTO students (name, age, gender) VALUES ('张三', 20, 'male');
This INSERT statement will insert a record into the students table, in which the value of the name field is 'Zhang San', the value of the age field is 20, and the value of the gender field for 'male'.
If you need to insert multiple records, you can also use the INSERT statement to insert multiple rows of data at one time. For example:
INSERT INTO students (name, age, gender) VALUES ('李四', 22, 'male'), ('王五', 21, 'female'), ('赵六', 23, 'male');
The above statement will insert three records into the students table at the same time, namely ('李思', 22, 'male'), ('王五', 21, 'female') and ( 'Zhao Liu', 23, 'male').
In addition, you can also use the INSERT...SELECT statement to select data from other tables and insert it into the target table, or use the INSERT IGNORE statement to ignore errors and continue when you encounter a duplicate primary key or a unique index conflict when inserting data. implement.
In short, by using the INSERT statement in MySQL, we can easily insert new data records into the database table, providing more flexibility and convenience for database management and operation.
The above is the detailed content of How to implement the statement of inserting data in MySQL?. For more information, please follow other related articles on the PHP Chinese website!