whats wrong with mysql tigger - mysql

This is the trigger im trying to add. I keep getting "Updating of NEW row is not allowed in after trigger"
DELIMITER $$
DROP TRIGGER IF EXISTS leaderboard.badges_AUPD$$
USE leaderboard$$
CREATE TRIGGER `badges_AUPD` AFTER UPDATE ON `badges` FOR EACH ROW
set new.badgelevel := case when badgepercent < 50 then 0
when badgepercent < 75 then 1
else 2 end;
$$
DELIMITER ;

Your trigger should be before update since after update you cant set any column of the same table, and also missing the begin part
DELIMITER $$
DROP TRIGGER IF EXISTS leaderboard.badges_AUPD$$
USE leaderboard$$
CREATE TRIGGER `badges_AUPD` BEFORE UPDATE ON `badges`
FOR EACH ROW
BEGIN
if new.badgepercent < 50 then
set new.badgelevel = 0 ;
elseif new.badgepercent < 75 then
set new.badgelevel = 1 ;
else
set new.badgelevel = 2;
end if;
end;$$
delimiter ;

Related

Mysql Trigger Error with IF THEN

I have a problem with trigger.. I would like to change a value to false if new returnBike is not null.
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
IF (NEW.returnBike <> null) THEN
UPDATE Bike SET isRented = false
WHERE id = (select Bike from Rent); -- but there is Error (missing semicolon)
END IF;
Any advice?
I would be inclined to just do:
DELIMITER $$
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
BEGIN
UPDATE Bike
SET isRented = false
WHERE id = new.Bike AND NEW.returnBike IS NOT NULL;
END; $$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
BEGIN
IF (NEW.returnBike IS NOT NULL) THEN
UPDATE BikeSET isRented = false
WHERE id = new.Bike;
END IF;
END; $$
DELIMITER ;
so to remove problem with syntax semicolon added delimiter and $$ after end;
Anyway thanks to give me some advice with if, comparing!

How to use trigger mysql for filter

I have a code trigger before insert in MySQLfor filter data but it doesn't work. This is the logical of my code, if new.suhu_udara > 30 and new.suhu_udara - old.suhu_udara <10 then set new.suhu_udara = null
USE `cuaca_maritim`;
DELIMITER $$
DROP TRIGGER IF EXISTS cuaca_maritim.filter$$
USE `cuaca_maritim`$$
CREATE DEFINER=`root`#`localhost` TRIGGER `filter` BEFORE INSERT ON data_cuaca` FOR EACH ROW
if( new.suhu_udara < 21.5 or new.kelembaban_udara < 22 or new.tekanan_udara < 1002.4) then
Set new.suhu_udara = null ;
elseif ( new.suhu_udara > 37.6 or new.kelembaban_udara > 100 or new.tekanan_udara >1018.9 or new.kecepatan_angin > 44) then
Set new.kelembaban_udara = null ;
end if$$
DELIMITER ;
USE `cuaca_maritim`;
DELIMITER $$
DROP TRIGGER IF EXISTS cuaca_maritim.data_cuaca_AFTER_UPDATE$$
USE `cuaca_maritim`$$
CREATE DEFINER = CURRENT_USER TRIGGER `cuaca_maritim`.`data_cuaca_AFTER_UPDATE` AFTER UPDATE ON `data_cuaca` FOR EACH ROW
if ( old.suhu_udara - new.suhu_udara > 10) then
set new.suhu_udara=null ;
end if
$$
DELIMITER ;
Can you help mex this code? thank you
You are not using proper trigger functionality. In Insert Trigger what is OLD.suhu_udara ?
OLD. values are only in the update queries.
Please check trigger functionalities, I think you should use Update here.

Loop in trigger after insert

I accidently deleted my trigger (Needed to reset something in my DB). But i don't know how to make my trigger again..
I have a 2 tables.
1:
train_information:
2:
axle:
Now, when a train gets added and inserted in the DB table: train_information. I want a trigger to also update the axle table.
After the trigger worked i want the axle table to look like:
However. If the train_information has 4 axles. i only want to put 3 in the axle table. So i want the loop/trigger to insert 1 less.
So if the train_information table has 20 axles. i want the axle table to show 19 etc.
You can have the following trigger as
delimiter //
create trigger train_information_ins after insert on train_information
for each row
begin
declare x int ;
if(new.number_of_axies > 0 ) then
set x = 1 ;
while x < new.number_of_axies do
insert into axle (train_id,axle)
values
(new.train_id,x);
set x=x+1;
end while ;
end if ;
end;//
delimiter ;
Below script will get you going. This script will loop through the number of axles which got inserted.
DELIMITER //
CREATE TRIGGER `train_information_after_insert` AFTER INSERT ON `train_information`
FOR EACH ROW
BEGIN
DECLARE noAxles INT;
SET noAxles = NEW.number_of_axles;
myLoop: LOOP
SET noAxles = noAxles - 1;
//UPDATE STATEMENT FOR AXLE TABLE HERE
IF noAxles = 0 THEN
LEAVE myLoop;
END IF;
END LOOP myLoop;
END//
DELIMITER ;

Problems creating TRIGGER in MySQL

I am first time using trigger concept in my MYSQL workbench like below query,
First I created people table by using below query,
CREATE TABLE people (age INT, name varchar(150));
Then I used below query for trigger
DELIMITER
CREATE TRIGGER agecheck BEFORE INSERT ON raptor1_5.people FOR EACH ROW IF NEW.age < 0 THEN SET NEW.age = 0; END IF;
DELIMITER ;
But agecheck trigger not creating to people table. Its does not show any error message when I run this query.
Here is my table images,
Updated : based on the answer
Try with this.
USE `raptor1_5`;
DELIMITER $$
DROP TRIGGER IF EXISTS raptor1_5.agecheck$$
USE `raptor1_5`$$
CREATE TRIGGER agecheck BEFORE INSERT ON people
FOR EACH ROW
BEGIN
IF NEW.age < 0 THEN
SET NEW.age = 0;
END IF;
END$$
DELIMITER ;
The standard trigger syntax should be as
DELIMITER //
CREATE TRIGGER agecheck BEFORE INSERT ON raptor1_5.people
FOR EACH ROW
BEGIN
IF NEW.age < 0 THEN
SET NEW.age = 0;
END IF;
END;//
DELIMITER ;
The above is when you run on mysql cli, however in phpmyadmin or workbench check if you have an option to set the delimiter if yes choose that and remove the delimiter part from the above.

Trigger not checking if condition

I have created a trigger , and this trigger updating the field value , but not checking the if condition ,
DELIMITER $$
create trigger `njsystem`.`test` BEFORE UPDATE on `njsystem`.`tbl_users`
for each row
BEGIN
IF (NEW.user_failed_logins > 3) THEN
UPDATE tbl_users SET user_active = 0;
END IF;
END; $$
DELIMITER ;
What I understand from your problem statement is that, you wanted to set user_active to 0 if user_failed_logins count goes above 3. Here is solution for that, you can also change values using new.
DELIMITER $$
create trigger `test` BEFORE UPDATE on `tbl_users`
for each row
BEGIN
IF (NEW.user_failed_logins > 3) THEN
SET NEW.user_active = 0;
END IF;
END; $$
DELIMITER ;