0

I have these tables in my database:

enter image description here

I want to add the registod and alarmes table one idRegisto .

The alarm table is populated automatically by a trigger. I would like to connect the two tables and the table alarmes populated idRegistos automatically by a trigger with the values ​​of table records.

Does anyone can help me please. I hope I have explained well my doubts

Thank you

enter image description here

My Trigger that populated table alarmes

DELIMITER $$
create TRIGGER alerta
BEFORE INSERT ON registos
FOR EACH ROW
begin
Set @comp=0;
Set @tempmax=0;
Set @tempmin=0;
Set @hummax=0;
Set @hummin=0;
Set @orvalho=0;


select lim_inf_temp, lim_sup_temp, lim_sup_humid, lim_inf_humid, lim_pt_orvalho into    @tempmin, @tempmax, @hummax, @hummin, @orvalho from sensores  where idSensor=NEW.idSensor;


Set @maxidAlarme=0;
if (CAST(NEW.Temperatura AS UNSIGNED)<@tempmin) then
SELECT MAX(idAlarme) into @maxidAlarme FROM alarmes;
SET @maxidAlarme=@maxidAlarme+1;
INSERT INTO alarmes(idAlarme,descricao_alarme) VALUES (@maxidAlarme,"ERROR");
end if; 


end $$;

DELIMITER  ;
8
  • Did you define referential constraint in alarm table on idRegisto column? How do you want to store it? Commented Mar 28, 2014 at 9:47
  • Thanks for reply. Not Yet. I can store as int... Commented Mar 28, 2014 at 10:09
  • Unless you add what you want on idRegisto, your posting is incomplete. Commented Mar 28, 2014 at 10:10
  • idRegisto will be automatically incremented. Is basically an id from table registos. Commented Mar 28, 2014 at 10:13
  • How do want to use it in alarm table? Using a trigger? And also see my first comment on top. Commented Mar 28, 2014 at 10:14

1 Answer 1

1

In alarm table, do you want to use the same newly generated idRegisto of registos table? - Ravinder

Yes. This is what i want. – user3320956

To insert the same newly generated idRegisto field value in alarm table,
Change part of your trigger body as below:

if ( CAST( NEW.Temperatura AS UNSIGNED ) < @tempmin ) then
     SELECT MAX( idAlarme ) into @maxidAlarme FROM alarmes;
     SET @maxidAlarme := @maxidAlarme + 1;

     SET @auto_idRegisto := ( SELECT AUTO_INCREMENT 
                              FROM INFORMATION_SCHEMA.TABLES
                              WHERE TABLE_NAME = 'registos'
                                AND TABLE_SCHEMA = DATABASE() );

     INSERT INTO alarmes( idAlarme, descricao_alarme, idRegisto ) 
     VALUES ( @maxidAlarme, "ERROR", @auto_idRegisto );
end if; 
Sign up to request clarification or add additional context in comments.

7 Comments

Very Thanks for help.Could you tell me how to change the alarm table to take the idRegisto as a foreign key reference to the table registos please?
I'll test the code that you kindly gave me. But first I need to link the two tables by foreign key. That's why I asked for help it.
I will acept your answer but before I need to test.
Could you tell me how to change the alarm table to take the idRegisto as a foreign key reference to the table registos please?
In Alarm table add the idRegisto column with the same data type and size as in registos table and add referential constraint as foreign key (idRegisto) references registos(idRegisto).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.