Increase the number of vehicles with Trigger

Asked

Viewed 40 times

0

I have the table pessoa who has the spine qtd_veiculos, every time I give one insert with a new vehicle, which has the CPF foreign_key same as some person at the table pessoa, this column should be incremented by 1, I’m not getting anywhere to do this.

Attempt 1:

CREATE OR REPLACE FUNCTION incrementa_qtd_veiculos() RETURNS TRIGGER AS $$
BEGIN
    UPDATE pessoa SET qtd_veiculos = qtd_veiculos+1 WHERE cpf = new.veiculo.cpf_dono;
    RETURN NULL;
END;
$$ LANGUAGE PLPGSQL;

Attempt 2:

CREATE TRIGGER incr_qtd_veiculos AFTER
INSERT ON pessoa
FOR EACH ROW EXECUTE PROCEDURE incrementa_qtd_veiculos();

P.S.: The database contains vehicle monitoring information.

  • I remastered your question to get more acceptance :)

  • It wouldn’t be AFTER INSERT ON vehicle?

  • 1

    It would no longer be practical for you to give a vehicle COUNT for that CPF every time you wanted such information?

No answers

Browser other questions tagged

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