Rechercher dans le manuel MySQL
28.7.29.3 How to Get the Unique ID for the Last Inserted Row
If you insert a record into a table that contains an
AUTO_INCREMENT
column, you can obtain the
value stored into that column by calling the
mysql_insert_id()
function.
You can check from your C applications whether a value was
stored in an AUTO_INCREMENT
column by
executing the following code (which assumes that you've checked
that the statement succeeded). It determines whether the query
was an INSERT
with an
AUTO_INCREMENT
index:
if ((result = mysql_store_result(&mysql)) == 0 &&
mysql_field_count(&mysql) == 0 &&
mysql_insert_id(&mysql) != 0)
{
used_id = mysql_insert_id(&mysql);
}
When a new AUTO_INCREMENT
value has been
generated, you can also obtain it by executing a SELECT
LAST_INSERT_ID()
statement with
mysql_query()
and retrieving the
value from the result set returned by the statement.
When inserting multiple values, the last automatically incremented value is returned.
For LAST_INSERT_ID()
, the most
recently generated ID is maintained in the server on a
per-connection basis. It is not changed by another client. It is
not even changed if you update another
AUTO_INCREMENT
column with a nonmagic value
(that is, a value that is not NULL
and not
0
). Using
LAST_INSERT_ID()
and
AUTO_INCREMENT
columns simultaneously from
multiple clients is perfectly valid. Each client will receive
the last inserted ID for the last statement
that client executed.
If you want to use the ID that was generated for one table and insert it into a second table, you can use SQL statements like this:
mysql_insert_id()
returns the
value stored into an AUTO_INCREMENT
column,
whether that value is automatically generated by storing
NULL
or 0
or was specified
as an explicit value.
LAST_INSERT_ID()
returns only
automatically generated AUTO_INCREMENT
values. If you store an explicit value other than
NULL
or 0
, it does not
affect the value returned by
LAST_INSERT_ID()
.
For more information on obtaining the last ID in an
AUTO_INCREMENT
column:
For information on
LAST_INSERT_ID()
, which can be used within an SQL statement, see Section 12.15, “Information Functions”.For information on
mysql_insert_id()
, the function you use from within the C API, see Section 28.7.7.38, “mysql_insert_id()”.For information on obtaining the auto-incremented value when using Connector/J, see Retrieving
AUTO_INCREMENT
Column Values through JDBC.For information on obtaining the auto-incremented value when using Connector/ODBC, see Obtaining Auto-Increment Values.
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-getting-unique-id.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.