Home >Database >Mysql Tutorial >What does the MySQL INSERT statement return?
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);
When this INSERT statement is executed, MySQL will return a result, mainly including the following situations:
Insert record successfully: If the insertion operation is successful, MySQL will return a result similar to the following:
Query OK, 1 row affected
This means that a row of data was successfully inserted.
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'
This means that the insertion Fails because the name field has a unique index and a record named 'John' already exists.
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();In this case, MySQL will return the auto-increment id value of the record just inserted.
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!