skip to Main Content

I created a new column named garagem_coberta in my table. I can´t create the trigger because mysql says: unknown system variable ‘garagem_coberta’. Can someone help me?

delimiter //
create trigger update_garagem_coberta
before update on apartamento
for each row 
begin
if OLD.tipo = 'cobertura' then
set garagem_coberta = 'sim'
where valor = new.valor
end if;
end ;
//delimiter ;

2

Answers


  1. DELIMITER //
    
    CREATE TRIGGER update_garagem_coberta
    BEFORE UPDATE ON apartamento
    FOR EACH ROW
    BEGIN
        IF OLD.tipo = 'cobertura' THEN
            SET NEW.garagem_coberta = 'sim';
        END IF;
    END//
    
    DELIMITER ;
    

    check out this documentation.

    Official example from documentation

    mysql> delimiter //
    mysql> CREATE TRIGGER upd_check BEFORE UPDATE ON account
           FOR EACH ROW
           BEGIN
               IF NEW.amount < 0 THEN
                   SET NEW.amount = 0;
               ELSEIF NEW.amount > 100 THEN
                   SET NEW.amount = 100;
               END IF;
           END;//
    mysql> delimiter ;
    
    Login or Signup to reply.
  2. CREATE TRIGGER update_garagem_coberta
    BEFORE UPDATE ON apartamento
    FOR EACH ROW
    SET NEW.garagem_coberta = CASE WHEN OLD.tipo = 'cobertura' 
                                   THEN 'sim'
                                   ELSE NEW.garagem_coberta
                                   END;
    

    This is single-statement form, it does not need in both DELIMITER reassigning and BEGIN-END block usage.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search