Home > Database > Mysql Tutorial > body text

What does the MySQL INSERT statement return?

王林
Release: 2024-03-01 12:54:04
Original
1161 people have browsed it

MySQL INSERT语句返回什么?

MySQL The INSERT statement is used to insert new records into the database. When the INSERT statement is executed, MySQL will return a result. The specific return content depends on the success of the insertion operation. The following is a specific code example:

Assume there is a table named employees, with fields including id, name and age , a record needs to be inserted:

INSERT INTO employees (name, age) VALUES ('John', 30);
Copy after login

When this INSERT statement is executed, MySQL will return a result, mainly including the following situations:

  1. Insert record successfully: If the insertion operation is successful, MySQL will return a result similar to the following:

    Query OK, 1 row affected
    Copy after login

    This means that a row of data was successfully inserted.

  2. Insertion failure: If the insertion operation fails for some reason, MySQL will return the corresponding error message, for example:

    ERROR 1062 (23000): Duplicate entry 'John' for key 'name'
    Copy after login

    This means that the insertion Fails because the name field has a unique index and a record named 'John' already exists.

  3. Return the auto-increment primary key value: If there is an auto-increment primary key field in the table, such as id, you can pass # after inserting the record ##LAST_INSERT_ID() function to obtain the auto-increment primary key value of the row just inserted. The example is as follows:

    INSERT INTO employees (name, age) VALUES ('Jane', 25);
    SELECT LAST_INSERT_ID();
    Copy after login
    In this case, MySQL will return the auto-increment id value of the record just inserted.

    In general, MySQL's INSERT statement usually returns a result after execution to indicate whether the operation was successful and provide relevant information.

    The above is the detailed content of What does the MySQL INSERT statement return?. 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
Popular Tutorials
More>
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!