'RATING TRIGGER IF RATING IS MORE THAN 5 THROW ERROR [closed]

I need to create sql trigger if rating is more than 5 throw error

CREATE TRIGGER RATING_VALUE
BEFORE INSERT ON HOC_Reviews
FOR EACH ROW
BEGIN
    IF new.rating > 5 THEN
        RAISERROR( "You can rate only from 1 to 5");
    END IF;
END;/


Solution 1:[1]

A trigger is not the way; use check:

create table HOC_Reviews (
    -- other columns
    rating int not null check (rating between 1 and 5),
    -- other columnS
)

Note the ease of reading when the check is coded using between and the implicit check for rating >= 1 being included, which you should also be making if you're checking rating <= 5.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1