Home  >  Article  >  Database  >  The use of insertinto statement in basic MySQL

The use of insertinto statement in basic MySQL

WBOY
WBOYOriginal
2024-02-18 19:39:06572browse

The use of insertinto statement in basic MySQL

Basic usage of insert into statement in MySQL

MySQL is a commonly used relational database management system, which provides a rich set of SQL statements for data query and operations. Among them, the insert into statement is used to insert new data into the table. The following will introduce the basic usage of the insert into statement in MySQL and provide specific code examples.

  1. Insert complete data
    Suppose we have a student table (student), which contains three fields: student number (id), name (name) and age (age). To insert a complete student data into this table, you can use the following insert into statement:

    insert into student(id, name, age) values (1, '张三', 18);

    The above code will insert a student number 1, name 'Zhang San', age into the student table 18 student data.

  2. Insert partial data
    Sometimes, we may only want to insert the values ​​of some fields, while the values ​​of other fields can be empty or use default values. At this time, you can omit the fields and corresponding values ​​​​that need to insert data in the insert into statement.
    Assume that the age field in the student table is set to the default value 20. To insert a piece of student data that only contains student number and name, you can use the following insert into statement:

    insert into student(id, name) values (2, '李四');

    The above code will be in student Insert a piece of student data into the table with a student number of 2, a name of 'Li Si', and an age of 20, which is the default value.

  3. Insert multiple pieces of data at the same time
    Sometimes we need to insert multiple pieces of data at once instead of inserting them one by one. MySQL provides a way to insert data in batches by specifying multiple values ​​in the insert into statement, with each value surrounded by parentheses and separated by commas.
    Suppose we need to insert three pieces of student data, we can use the following insert into statement:

    insert into student(id, name, age) values (3, '王五', 19), (4, '赵六', 20), (5, '刘七', 21);

    The above code will insert three pieces of student data into the student table, with student number 3 and name 'Wang Wu' ', age is 19; student number is 4, name is 'Zhao Liu', age is 20; student number is 5, name is 'Liu Qi', age is 21 student data.

  4. Insert query results
    Sometimes we need to insert the query results into another table, we can use the insert into select statement. This method makes it easy to copy the query results to other tables.
    Suppose we have a temporary table temp, which contains two fields: student number and grade. We want to insert the student data with scores greater than 80 in the temp table into the student table. We can use the following insert into select statement:

    insert into student(id, name, age) select id, name, age from temp where score > 80;

    The above code will query the student data with scores greater than 80 in the temp table and insert The results are inserted into the student table.

Through the above examples, we understand the basic usage of the insert into statement in MySQL. According to actual needs, we can insert complete data, insert partial data, insert multiple pieces of data at the same time, and even insert query results into other tables. In actual database operations, we need to use the insert into statement according to the specific table structure and data requirements.

The above is the detailed content of The use of insertinto statement in basic MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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