title: MySQL trigger after insert and after update
body: <p>I have two tables with one named <code>att</code> as follows<br></p>
  
  <pre><code>CREATE TABLE att (
    SID varchar(50) NOT NULL, 
    CID varchar(50) NOT NULL, 
    Date date NOT NULL,
    H1 varchar(1) NOT NULL DEFAULT 0,
    H2 varchar(1) NOT NULL DEFAULT 0,
    H3 varchar(1) NOT NULL DEFAULT 0,
    H4 varchar(1) NOT NULL DEFAULT 0,
    H5 varchar(1) NOT NULL DEFAULT 0,
    H6 varchar(1) NOT NULL DEFAULT 0,
    H7 varchar(1) NOT NULL DEFAULT 0,
    H8 varchar(1) NOT NULL DEFAULT 0,
    H9 varchar(1) NOT NULL DEFAULT 0,
    H10 varchar(1) NOT NULL DEFAULT 0,
    INDEX (SID, CID)
  );
  </code></pre>
  
  <p>The other table is <code>per</code> with following fields:<br>
  SID CID Per<br></p>
  
  <p>How do I write a trigger for the following:<br>
  If an update occurs in any of the fields from h1-h0 on <code>att</code> table then<br>
  update the <code>per</code> column in the <code>per</code> table with the following values:<br>
  ((total no of 1s - total no of 0s)/(total no of 1s + total no of 0s))/100<br></p>
  
  <p>Thanks in advance<br>
  i Developed a Trigger,but it not working,its saying error in line 11,can you say what is the problem??<br></p>
  
  <pre><code>create TRIGGER `att_up` AFTER UPDATE ON `attentance`
  FOR EACH ROW BEGIN
    DECLARE Zeros INT;
    DECLARE Ones INT;
    DECLARE total INT;
    DECLARE atted FLOAT;
    SELECT SUM(8-(h1+h2+h3+h4+h5+h6+h7+h8)) 
        INTO Zeros FROM attentance 
        WHERE StudID=NEW.StudID;
    SELECT SUM(h1+h2+h3+h4+h5+h6+h7+h8) 
        INTO Ones FROM attentance 
        WHERE StudID=NEW.StudID;
    SELECT SUM(8-(h1+h2+h3+h4+h5+h6+h7+h8))+ SUM(h1+h2+h3+h4+h5+h6+h7+h8) 
        INTO total FROM attentance 
        WHERE StudID=NEW.StudID;
    set atted=((ZEROS-Ones)/total)/100;
    INSERT into per(per) values (atted);
  END$$
  </code></pre>