title: MySQL Trigger to Increment / Decrement Value in Other Table
body: <p>I have two separate tables keeping track of teams in a league and games that the teams play. I have a field in the table 'team' for the wins for each team and am trying to create a trigger in MySQL to automatically update this field whenever a game is inserted/updated with a winning team, and conversely decrement it whenever a game is updated/deleted. These are my tables:</p>
  
  <pre><code>CREATE TABLE team (
  teamName varchar(32) NOT NULL,
  teamManager varchar(32) NOT NULL,
  teamPoints decimal(6,2) DEFAULT 0,
  teamWins tinyint(255) unsigned DEFAULT 0,
  leagueID tinyint(255) unsigned NOT NULL,
  FOREIGN KEY (leagueID) REFERENCES league (leagueID),
  PRIMARY KEY (teamName, leagueID));
  
  
  CREATE TABLE game (
  gameID int NOT NULL AUTO_INCREMENT,
  gameDate date NOT NULL,
  winningTeam varchar(32) NOT NULL,
  losingTeam varchar(32) DEFAULT NULL,
  leagueID tinyint(255) unsigned NOT NULL,
  FOREIGN KEY (leagueID) REFERENCES league (leagueID),
  FOREIGN KEY (winningTeam) REFERENCES team (teamName),
  FOREIGN KEY (losingTeam) REFERENCES team (teamName),
  PRIMARY KEY (gameID));
  </code></pre>
  
  <p>and my attempted trigger for adding:</p>
  
  <pre><code>CREATE TRIGGER addWin
  AFTER INSERT, UPDATE ON game
  AS
  BEGIN
      UPDATE team
      SET team.teamWins = team.teamWins + 1
      WHERE team.teamName IN (SELECT inserted.winningTeam FROM inserted)
  END;
  </code></pre>