You can retrieve the most recent AUTO_INCREMENT value with the LAST_INSERT_ID(). : AUTO_INCREMENT : Table MySQL TUTORIALS


MySQL TUTORIALS » Table » AUTO_INCREMENT »

 

You can retrieve the most recent AUTO_INCREMENT value with the LAST_INSERT_ID().


LAST_INSERT_ID() is connection-specific.

mysql>
mysql>
mysql> CREATE TABLE employee (
    ->      id MEDIUMINT NOT NULL AUTO_INCREMENT,
    ->      name CHAR(30NOT NULL,
    ->      PRIMARY KEY (id)
    ->  );
Query OK, rows affected (0.05 sec)

mysql>
mysql> desc employee;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | mediumint(9| NO   | PRI | NULL    | auto_increment |
| name  | char(30)     | NO   |     |         |                |
+-------+--------------+------+-----+---------+----------------+
rows in set (0.01 sec)

mysql>
mysql> INSERT INTO employee (nameVALUES ('A');
Query OK, row affected (0.00 sec)

mysql>
mysql> SELECT FROM employee;
+----+------+
| id | name |
+----+------+
|  | A    |
+----+------+
row in set (0.00 sec)

mysql>
mysql> select LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                |
+------------------+
row in set (0.00 sec)

mysql>
mysql> drop table employee;
Query OK, rows affected (0.00 sec)

mysql>
mysql>



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .

Follow Navioo On Twitter

MySQL TUTORIALS

 Navioo Table
» AUTO_INCREMENT