0

My goal in general is like that: I have a a table with many translations - each translation have a rating - the average of all ratings from the user. each translation have a unique Id, and in the second table I am saving for each user that ranked a translation the rating he\she gave (userId, translationId, rating)

now everytime a user rank a new translation or change his old rate I want to update the average rating of this translation

I have the following tables:

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

this means that everytime a new row is inserted into 'TranslationsRating' or I update the rating column in this table I want to take the translationId of the row that was just changed and in the 'translation' table update this translationId rating (find all rating for this Id in 'TranslationsRating' and calc the average)

I'm trying to define the following trigger:

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;

and I'm getting the following error message (I'm using phpMyAdmin):

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

my question is what is this error, and if my trigger is written in a good way that will achive my goal

4

0 回答 0