← Back to team overview

maria-discuss team mailing list archive

Re: When using the getGeneratedKeys() method the keys are returned, but in which order?

 

Hi,

You can not get all of the generated keys from a bulk insert, only the last
one, because the id value is made available through the last_insert_id()
function, and the client API value to return the last insert id works the
same way.

You will either have to do single value (not bulk) inserts, or you will
have to lock the table, select the maximum id, insert the values, select
the new maximum value, and then unlock the table.  This will give you the
complete set of values for the block of id values assigned to the bulk
insert.

--Justin

On Thu, Aug 25, 2016 at 10:38 AM, Péter Éberhardt <eberhardt.peter@xxxxxxxxx
> wrote:

> Hi,
>
> I want to insert multiple records into a table using one statement
> like this through jdbc:
>
>   INSERT INTO demo (name) VALUES ('Jeff'),('Jones'),('Agnes'),.
> ..,('Wolfgang');
>
> Then I want to get the generated ids with the getGeneratedKeys()
> method and match them to the inserted records but how can I be sure
> the method gives the ids in the same order as I listed the records in
> the statement? I can't find any related information in the
> documentation just the fact the method gives back those ids.
>
> DDL:
>
> CREATE TABLE demo (id bigint primary key auto_increment , name text);
>
> If this information would put into the document also then I can go
> with my implementation on this way.
>
> Thanks,
> Peter
>
> _______________________________________________
> Mailing list: https://launchpad.net/~maria-discuss
> Post to     : maria-discuss@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~maria-discuss
> More help   : https://help.launchpad.net/ListHelp
>

References