定义新的触发器sql错误

时间:2013-04-12 21:29:30

标签: mysql sql triggers phpmyadmin

我的目标一般是这样的: 我有一个包含许多翻译的表格 - 每个翻译都有一个评级 - 来自用户的所有评级的平均值。 每个翻译都有一个唯一的ID,在第二个表格中,我为每个用户保存了一个翻译他/她给出的评分(userId,translationId,rating)

现在,每当用户对新翻译进行排名或更改旧费率时,我想更新此翻译的平均评分

我有以下表格:

Create Table if not exists Translation
        (TranslationID int (12) NOT NULL UNIQUE AUTO_INCREMENT,
        UserID int (10) NOT NULL, 
        ImageID int (10) NOT NULL, 
        ChainID int (2) NOT NULL,
        Translation text,
        TranslationRating double Default 0 CHECK (TranslationRating>=0 AND TranslationRating<=5),
        NumOfRatings int (10) Default 0,
        CONSTRAINT translations PRIMARY KEY (UserID,ImageID, ChainID),
        FOREIGN KEY (UserID) REFERENCES Users(UserID) ON DELETE CASCADE,
        FOREIGN KEY (ImageID) REFERENCES Images(ImageID) ON DELETE CASCADE)

Create Table if not exists TranslationsRating
        (UserID int (10) NOT NULL , 
        TranslationID int (3) NOT NULL,
        Rating double Default 0 CHECK (TranslationRating>=0 AND TranslationRating<=5),
        CONSTRAINT known_lang PRIMARY KEY (UserID,TranslationID),
        FOREIGN KEY (UserID) REFERENCES Users(UserID) ON DELETE CASCADE,
        FOREIGN KEY (TranslationID) REFERENCES Translation(TranslationID) ON DELETE CASCAD

这意味着每次在“TranslationsRating”中插入新行或我更新此表中的评级列时,我想获取刚刚更改的行的translationId,并在“翻译”表中更新此translationId评级(在'TranslationsRating'中找到该Id的所有评级并计算平均值

我正在尝试定义以下触发器:

CREATE TRIGGER UPDATE_TRANS_RATING
AFTER UPDATE ON TRANSLATIONSRATING
FOR EACH ROW
BEGIN
UPDATE TRANSLATION SET TRANSLATIONRATING = (SELECT AVG(RATING) FROM TRANSLATIONSRATING 
WHERE TRANSLATIONSRATING.TRANSLATIONID = RATINGNEW.ID)
END;

我收到以下错误消息(我正在使用phpMyAdmin):

MySQL said: #1303 - Can't create a TRIGGER from within another stored routine

我的问题是这个错误是什么,如果我的触发器以良好的方式编写,将实现我的目标

0 个答案:

没有答案