Create Trigger after update in the table itself and in another table

Asked

Viewed 1,299 times

1

First I have a table:

create table alecrim(
id_alecrim int not null auto_increment, 
sem_epi int not null unique,
p1 smallint,
p2 smallint,
p3 smallint,
p4 smallint,
p5 smallint, 
p6 smallint,  
p7 smallint,
p8 smallint,
p9 smallint,
totOvos int,
ano varchar(4),
primary key(id_alecrim)
) default charset = utf8;

And the table:

create table tb_indices_leste(
id_leste int not null auto_increment,
localidade varchar(30),
sem_epi int not null unique,
totOvos smallint,
pend tinyint,
ext tinyint,
ipo decimal(5,1),
ido decimal(5,1),
ano varchar(4),
primary key(id_leste)) default charset = utf8;

And I want to create a Trigger in order to after an UPDATE in the ROSEMARY table the Trigger "Setar" the totOvos field with the sum of the values of the fields P1+P2+...+P9, but this sum needs to be the line where you hear the UPDATE and "Setar" the same value in the totOvos field of table tb_indices_rosemary in the row where the value of sem_epi is equal to the value of sem_epi of ROSEMARY in which the UPDATE occurred. Could someone help me with this?

1 answer

1

Just use the BEFORE UPDATE:

CREATE TRIGGER trigger_alecrim_update BEFORE UPDATE 
ON alecrim
FOR EACH row
    SET NEW.totOvos = NEW.p1 + NEW.p2 + NEW.p3 + NEW.p4 + NEW.p5 + NEW.p6 + NEW.p7 + NEW.p8 + NEW.p9;
  • It worked perfectly, thank you very much, Felipe Marinho! And if I want to put another SET, just put below or need to create delimiters or BEGIN/END blocks?

  • @Amiraldorodriguesfilho You can use the same SET, just put a comma, column name, equals symbol and desired value, example: SET NEW.coluna1=valor1, NEW.coluna2=valor2, etc..

  • Felipe Marinho In the case of the other SET, I forgot to specify that it would be in another table! In fact it would be this SET in the same table and another for another one. It would need delimiters or other Trigger?

  • @Amiraldorodriguesfilho What is the relationship between the two tables? Which row of the table tb_indices_leste should be updated when one of the alecrim were?

  • The relation is (1:N), because the totOvos field influences the fields ipo,ido and totOvos of the table tb_indices_leste and the lines that have the same value in sem_epi are the lines that need to be updated.

  • @Amiraldorodriguesfilho sem_api would be a Foreign key table alecrim on the table tb_indices_leste? Would that be?

  • I have little knowledge of sql. If I need to make these changes, I do. So for this Rigger to work I need to do these relations on the tables?! Would it be better to define the sem_epi field to be Primary Keys and the foreing Keys? Because the other fields may change, but sem_epi cannot and is the common field between tables.

  • @Amiraldorodriguesfilho How there is a 1:N relationship between alecrim and tb_indices_leste, the ideal would be to have a column (Foreign key) in tb_indices_leste with the identifier of alecrim related (or another table that stores the relation). Without this, you will not know which lines should change in tb_indices_leste when changing some line in alecrim.

Show 3 more comments

Browser other questions tagged

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