Using last_insert_id(), is it credivel if several users enter at the same time in the database?

Asked

Viewed 656 times

2

I have a doubt about the method last_insert_id() mysql. And the following: I am creating a procedure to insert into 3 tables, example:

person (idPessoa, surname,)

employee(idEmployment,idPessoa,)

contact(idContact,idPessoa,)

So my procedure inserts data into these two tables and I’m using the last_insert_id() to recover the idPessoa table Person, to use in the two tables employed and contact.

CREATE DEFINER=user`@`localhost PROCEDURE inserirPessoa (var_nome
 varchar(45), var_apelido varchar(45), var_cargo varchar(45), var_celular bigint)

BEGIN

    INSERT INTO pessoa (nome, apelido) values (var_nome, var_apelido);
    
   
    SELECT LAST_INSERT_ID() into @idPessoa;


    INSERT INTO candidato (idPessoa, Cargo) values (@idPessoa, var_Cargo);


    INSERT INTO contacto (idPessoa, celular) values (@idPessoa, var_celular);

END

The code works well. My doubt is, thinking about the worst case, if more than one user submits the registration of this data, will the idPessoa Isn’t he vulnerable to recovery by the wrong employee??? How This Works last_insert_id()?

1 answer

3


The Mysql manual makes it clear that LAST_INSERT_ID is by connection thus it is perfectly safe to use it the way you are using independent of any transaction isolation setting.

  • Thank you, Sergio Garcia, I will try to read the manual of Mysql... Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.