两个触发呼叫相互呼叫

时间:2017-02-07 19:46:43

标签: mysql database database-trigger

所以我有两个表都有一些触发器,都是AFTER INSERT;我不知道如何调用它,但我希望标题不是那么不准确。 :) 我目前不熟悉触发器,所以请耐心等待。 会发生的是,对于每个新剧集条目,我需要更新每季剧集的数量。当季节表中的新条目创建新剧集(预告片)时。 我目前正在使用MySQL 5.7.17;这触发了DB2中的工作,但在MySQL中,我无法让它们工作。 我只是不断收到错误消息,说明我无法更新调用该语句的表:

  

无法更新表格' season'在存储的函数/触发器中,因为它已被调用此存储函数/触发器的语句使用。

如果你能帮助我,无论如何,无论如何都要提前感谢!

CREATE TABLE episodes (
  episodeId INTEGER NOT NULL AUTO_INCREMENT,
  seasonId INTEGER NOT NULL,
  number INTEGER NOT NULL,
  date DATE NOT NULL,
  title VARCHAR(256) NOT NULL,
  summary TEXT,
  PRIMARY KEY(episodeId)
);

CREATE TABLE seasons (
  seasonId INTEGER NOT NULL AUTO_INCREMENT,
  startDate DATE NOT NULL,
  name VARCHAR(128) DEFAULT 'Unknown season',
  numberOfEpisodes SMALLINT NOT NULL DEFAULT 0,
  PRIMARY KEY(seasonId)
);

/* TRIGGERS */
/* trigger to increment the numberOfEpisodes in seasons */
CREATE TRIGGER newEpisode
  AFTER INSERT ON episodes
  FOR EACH ROW
  BEGIN  -- I also tried without the Begin-End statement
    UPDATE seasons SET numberOfEpisodes = numberOfEpisodes + 1
    WHERE seasons.seasonId = NEW.seasonId;
  END;
;

/* Each season has a trailer as episode 0 */
CREATE TRIGGER newSeason
  AFTER INSERT ON seasons
  FOR EACH ROW
  BEGIN -- I also tried without the Begin-End statement
    INSERT INTO episodes (seasonId, number, date, title, summary)
      VALUES (NEW.seasonId, 0, CURRENT_TIMESTAMP, 'Trailer', 'Episode 0 = Trialer');
  END;
;

0 个答案:

没有答案