Create Trigger update MySQL

2

I have two tables one apartment to another contract. When the status of the contract is changed to off . The status of the apartment is free. I have a foreign key in the contract table to relate to apartment.

I would like to know how to create a trigger to update an apartment table field after updating a contract table field.

DELIMITER // 
CREATE TRIGGER upd_tab_b BEFORE UPDATE ON tab_b FOR EACH ROW BEGIN 
UPDATE tab_a 
SET campo_a = livre 
WHERE 1 = 1; 
-- a dúvida é como dizer atualize --quando o campo da tabela B for --igual a off
END;
// DELIMITER ;
    
asked by anonymous 01.08.2016 / 02:57

1 answer

3

In practice, to set the tab_a.campo_a = tab_b.campo_b + 1 field would look something like this:

DELIMITER //
CREATE TRIGGER upd_tab_b BEFORE UPDATE ON tab_b
FOR EACH ROW
BEGIN
    UPDATE tab_a SET campo_a = NEW.campo_b + 1
    WHERE 1 = 1; -- informe suas condições
END;//
DELIMITER ;

It's worth taking a look at the syntax of a trigger .     

01.08.2016 / 13:08