Mysql
 sql >> Database >  >> RDS >> Mysql

Trigger MySQL dopo l'inserimento e dopo l'aggiornamento

Assicurati di modificare il delimitatore prima di definire il trigger. Assicurati inoltre di utilizzare gli stessi nomi di tabella e colonna quando crei la tabella e l'attivatore (stai utilizzando att e attendance e SID e StudID , nei tuoi esempi).

Così com'è, la definizione del trigger non ha causato errori quando l'ho testata in MySQL 5.1.55 dopo aver impostato il delimitatore.

delimiter $$
CREATE TRIGGER `att_up` 
  AFTER UPDATE ON `attendance`
FOR EACH ROW 
BEGIN
  DECLARE Zeros INT;
  DECLARE Ones INT;
  DECLARE total INT;
  DECLARE attend FLOAT;
  SELECT SUM(8-(h1+h2+h3+h4+h5+h6+h7+h8)), 
         SUM(h1+h2+h3+h4+h5+h6+h7+h8),
         SUM(8-(h1+h2+h3+h4+h5+h6+h7+h8)) + SUM(h1+h2+h3+h4+h5+h6+h7+h8)
    INTO Zeros, Ones, Total FROM attendance 
    WHERE SID=NEW.SID;
  SET attend=((Zeros-Ones)/total)/100;
  INSERT INTO per (SID, CID, per) values (NEW.SID, NEW.CID, attend)
    ON DUPLICATE KEY UPDATE per=attend;
END$$
delimiter ;