I have a question regarding the last_insert_id()
method of mysql.
And the following: I am creating a procedure to insert into 3 tables, example:
person (idPerson, first name, last name)
employee (idEmpregado, idPessoa, cargo)
contact (idContact, idP, cell)
So, my procedure inserts data into these two tables and I'm using last_insert_id()
to retrieve the idPessoa
of the Person table, to use both employee and contact tables.
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 fine. My doubt is, considering the worst case, if more than one user submits the data, will idPessoa
not be vulnerable to being recovered by the wrong employee?
How does this last_insert_id()
work?