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()
?
This will depend on the type of isolation of the transaction, Mysql Transactions written in the same table
– rray