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






LAST_INSERT_ID() is connection-specific.

mysql>
mysql>
mysql> CREATE TABLE employee (
    ->      id MEDIUMINT NOT NULL AUTO_INCREMENT,
    ->      name CHAR(30) NOT NULL,
    ->      PRIMARY KEY (id)
    ->  );
Query OK, 0 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   |     |         |                |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)

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

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

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

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

mysql>
mysql>








4.18.AUTO_INCREMENT
4.18.1.Use Auto_increment column as part of the key
4.18.2.To start with an AUTO_INCREMENT value other than 1
4.18.3.You can retrieve the most recent AUTO_INCREMENT value with the LAST_INSERT_ID().
4.18.4.For a multiple-row insert, LAST_INSERT_ID() returns the AUTO_INCREMENT key from the first of the inserted rows