我有两个单独的表,用于跟踪联赛中的球队和球队所参加的比赛。我在“团队”表中有一个字段,用于表示每个团队的获胜情况,并且正在尝试在MySQL中创建触发器,以便在与获胜团队一起插入/更新游戏时自动更新此字段,反之则在游戏进行时将其递减更新/删除。这些是我的表格:
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));
以及我尝试添加的触发器:
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;
答案 0 :(得分:0)
您的触发器看起来像SQL Server触发器。在MySQL中,它看起来更像是:
DELIMITER $$
CREATE TRIGGER addWin AFTER INSERT ON game
AS
BEGIN
UPDATE team
SET teamWins = teamWins + 1
WHERE teamName = new.winningTeam;
END;$$
DELIMITER ;
更新触发器将更加复杂,因为您必须撤消之前的增量。我留给你。