Home > Database > Mysql Tutorial > What is the use of the MySQL LAST_INSERT_ID() function?

What is the use of the MySQL LAST_INSERT_ID() function?

WBOY
Release: 2023-09-20 13:41:02
forward
798 people have browsed it

MySQL LAST_INSERT_ID() 函数有什么用?

MySQL LAST_INSERT_ID() function is used to obtain the recently generated sequence number through AUTO_INCRMENT.

Example

In this example, we are creating a table named "Student" that has the AUTO_INCRMENT column. We insert two values ​​in the "Name" column and when we use the INSERT_LAST_ID() function, it returns the most recently generated sequence number, which is 2.

mysql> Create table Student(Id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, Name Varchar(5));
Query OK, 0 rows affected (0.13 sec)

mysql> Insert into student(Name) Values('Raman');
Query OK, 1 row affected (0.06 sec)

mysql> Insert into student(Name) Values('Rahul');
Query OK, 1 row affected (0.07 sec)

mysql> Select* from student;
+----+-------+
| Id | Name |
+----+-------+
| 1 | Raman |
| 2 | Rahul |
+---+-------+
2 rows in set (0.00 sec)

mysql> Select Last_insert_id();
+------------------+
| Last_insert_id() |
+------------------+
|                2 |
+------------------+
1 row in set (0.00 sec)
Copy after login

The above is the detailed content of What is the use of the MySQL LAST_INSERT_ID() function?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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