this is the error i am getting either insert alone and update alone works why is insert or update not working for the trigger ?
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
OR INSERT ON review FOR EACH ROW BEGIN DECLARE ratings INT; SET ratings = NE
' at line 2
here is the code for creating database
drop database if exists nithin;
create database nithin;
use nithin;
create table artist (
artistid int primary key,
artistfname varchar(50),
artistlname varchar(50),
salary int(10)
);
create table review (
filmid int,
noofstarratings int,
);
INSERT INTO review (filmid, noofstarratings)
VALUES (1, 4),
(2, 3),
(3, 5);
delimiter /
drop procedure if exists LimitStarRatings;
create procedure LimitStarRatings(IN ratings INT)
BEGIN
if ratings>10 then set ratings =10; end if;
if ratings<0 then set ratings =0; end if;
select ratings;
END;
-- the query thats givin me headache
drop trigger if exists LimitRatingTable/
create trigger LimitRatingTable
Before insert or update on review
for each row
begin
declare ratings int;
set ratings = new.noofstarratings;
call LimitStarRatings(ratings);
new.noofstarratings=ratings;
end; /
delimiter ;
i was expecting it to work together but it seems to work alone ig
A MySQL trigger must name only one event. There is no syntax for BEFORE INSERT OR UPDATE
. You need to create a separate trigger for each, even if the code in the body of both triggers is identical.
https://dev.mysql.com/doc/refman/8.0/en/create-trigger.html shows the syntax reference:
CREATE
[DEFINER = user]
TRIGGER [IF NOT EXISTS] trigger_name
trigger_time trigger_event
ON tbl_name FOR EACH ROW
[trigger_order]
trigger_body
trigger_time: { BEFORE | AFTER }
trigger_event: { INSERT | UPDATE | DELETE }
In this syntax notation, trigger_event
is replaced by one of INSERT
, UPDATE
, or DELETE
. Not multiple events.
There is no OR
operation in that syntax in MySQL, even if it is supported in some other brand of SQL database.
In any programming language, the syntax is fixed, according to what the implementation is. You can't just make up new syntax that is not part of the implementation, and expect it to do what you want.
Keep in mind there is a lot of variation between different brands of SQL databases. In spite of the fact that SQL is an ANSI/ISO standard, each vendor makes their own choices about which features to implement. Also, all vendors add features that are not part of the SQL standard.
This is all to say that you need to study the documentation for the brand and version of product you are currently using.