How to implement the statement of inserting data in MySQL?

WBOY
Release: 2023-11-08 11:48:27
Original
1322 people have browsed it

How to implement the statement of inserting data in MySQL?

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, ...);
Copy after login

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 );
Copy after login

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');
Copy after login

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');
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!