|
Not much time today, so here's the brief solution on how to get the generated key after performing an SQL INSERT statement using MySQL and an auto_increment field. (Some databases also refer to this as an identity field.)
The short answer is that you want to use the MySQL LAST_INSERT_ID() function, calling it a little like this immediately after performing your INSERT command (and using the same connection):
SELECT LAST_INSERT_ID();
Here's a blurb from this MySQL page:
... LAST_INSERT_ID() (with no argument) returns the first automatically generated value that was set for an AUTO_INCREMENT column by the most recently executed INSERT statement to affect such a column.
Please read that page that I linked to for more information. Also, remember that if you're using a framework like Spring, it will probably have convenience functions that you can use to get the generated key from an auto_increment field.
|