Loop in trigger after insert - mysql

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 ;

Related

MySQL trigger, change value before update conditionally

I'm trying to change a value conditionally on my trigger, but I've got an error.
My trigger is:
CREATE TRIGGER `defineBase` BEFORE INSERT ON `perguntas`
FOR EACH ROW
BEGIN
IF NEW.per_base = 0 THEN
SET NEW.per_base = (SELECT per_id FROM perguntas ORDER BY per_id DESC LIMIT 1) + 1;
END IF;
END;
but doesn't work.
You need to change the delimiter to something else than ;. Otherwise the trigger definition stops at the first ;
delimiter |
CREATE TRIGGER `defineBase` BEFORE INSERT ON `perguntas`
FOR EACH ROW
BEGIN
IF NEW.per_base = 0 THEN
SET NEW.per_base = (SELECT per_id FROM perguntas ORDER BY per_id DESC LIMIT 1) + 1;
END IF;
END
|
delimiter ;
I also have the same problem. My SQL code before is just like this (without delimiter):
CREATE TRIGGER update_created_time BEFORE INSERT
ON contact FOR EACH ROW
BEGIN
SET NEW.created=NOW();
END
Then, after i add the following DELIMITER // and close it with the same //
DELIMITER //
CREATE TRIGGER update_created_time BEFORE INSERT
ON contact FOR EACH ROW
BEGIN
SET NEW.created=NOW();
END //
It works. I hope it can help someone in the future...

Mysql update trigger change value

I have a table with a couple of columns;
datetime_end,
datetime_start,
duration
When the light is switched on, the row is inserted and only datetime_start is filled. When the light is switched off the datetime_end and duration are filled.
I want a trigger that changes the duration with 40 seconds (duration=duration-40) once the datetime_end is written.
DELIMITER //
CREATE TRIGGER trigger_light_test
BEFORE UPDATE ON light_test
FOR EACH ROW
BEGIN
IF OLD.duration > 50 THEN
SET NEW.duration = OLD.duration - 45;
END IF;
END; //
DELIMITER ;
The trigger is created just fine but doesn't do anything.. even if the light is on for 70 secs.. any ideas?
OLD contains the record from the table before the UPDATE is issued. In this case, that's NULL, so you're always comparing NULL > 50 which always evaluates to FALSE. You want to be comparing to the value in NEW, which is the value of the record as it will be, and which is the value you manipulate in the trigger.
DELIMITER //
CREATE TRIGGER trigger_light_test
BEFORE UPDATE ON light_test
FOR EACH ROW
BEGIN
IF NEW.duration > 50 THEN
SET NEW.duration = NEW.duration - 45;
END IF;
END; //
DELIMITER ;
Long time I used trigger but, if I understood your question correctly, you want the trigger to run after the update.
Wouldn't it be :
DELIMITER //
CREATE TRIGGER trigger_light_test
AFTER UPDATE ON light_test
FOR EACH ROW
BEGIN
IF OLD.duration > 50 THEN
SET NEW.duration = OLD.duration - 45;
END IF;
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.

whats wrong with mysql tigger

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 ;

while loop terminating after fetching some data

I have written a procedure that creates a temporary table and executes a query by fetching the rows from the temporary table.I have around 13486 rows in the temporary table.But when i am calling the procedure i observed that the procedure is getting terminated after fetching 107 rows from the temporary table.Moreover i also observed that this value is not constant..Sometimes it is 107 the other time it is 114 and some other time it is just 100.Why this happens?Please need help?Somebody please..Here is my procedure.And i came to know that while loop will terminate for >1000 iterations.Please suggest me a method to overcome this.
DELIMITER $$
DROP PROCEDURE IF EXISTS `lookup`.`test` $$
CREATE PROCEDURE `lookup`.`test` ()
BEGIN
CREATE TEMPORARY TABLE lookup.airportname(id int AUTO_INCREMENT,PRIMARY KEY(id))
AS (select distinct airport_id from lookup.airport);
SET #num=0;
SET #arpt=NULL;
SELECT count(*) INTO #num FROM airportname;
SET #i=0;
while #i<#num do
SELECT airport_id INTO #arpt FROM airportname WHERE id=#i;
select #arpt,#i;
set #i=#i+1;
end while;
END $$
DELIMITER ;
I am using mysql query browser.Thank you.
If you want to insert value into id Then it should not be auto_increment. - Remove auto_increment from table definition, it should work
delimiter |
create procedure employee_select()
Begin
Declare empno,done int(9);
Declare emp_select Cursor for select emp_no from gross_salary ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
Open emp_select; // cursor opening
read_loop: loop // start looping all the datas one by one
fetch emp_select into empno; // fetching the select value into variable empno
//note :variable name should not be same as columne name in select statement"
IF done THEN
LEAVE read_loop; // if no more rows, this makes it to leave the loop"
END IF;
//Enter the code you want do for each row
end loop;
close emp_select;
End |
delimiter ;