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?
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?
– Amiraldo Rodrigues Filho
@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
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?
– Amiraldo Rodrigues Filho
@Amiraldorodriguesfilho What is the relationship between the two tables? Which row of the table
tb_indices_leste
should be updated when one of thealecrim
were?– Felipe Marinho
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.
– Amiraldo Rodrigues Filho
@Amiraldorodriguesfilho
sem_api
would be a Foreign key tablealecrim
on the tabletb_indices_leste
? Would that be?– Felipe Marinho
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.
– Amiraldo Rodrigues Filho
@Amiraldorodriguesfilho How there is a 1:N relationship between
alecrim
andtb_indices_leste
, the ideal would be to have a column (Foreign key) intb_indices_leste
with the identifier ofalecrim
related (or another table that stores the relation). Without this, you will not know which lines should change intb_indices_leste
when changing some line inalecrim
.– Felipe Marinho