MySQL trigger, change value before update conditionally - mysql

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...

Related

Update one mysql column when another is edited

I'm trying to make a datetime field that automatically gets updated with the current time only if there was a change to a certain field.
It seems I have a syntax error.
CREATE OR ALTER TRIGGER last_progress_date
ON wp_task_mgr
AFTER UPDATE
AS BEGIN
IF UPDATE (progress_percentage)
SET last_progress_date = GETDATE()
END
Just for future reference, I found the answer here:
https://dba.stackexchange.com/questions/125203/simple-trigger-to-update-two-columns-in-one-table
MySQL query:
DELIMITER //
CREATE TRIGGER trig_1 before insert
ON <table_name> FOR EACH ROW
BEGIN
IF new.due_date is not null and new.end_date='' then
set new.end_date=new.due_date;
end if;
IF new.end_date is not null and new.due_date='' then
set new.due_date=new.end_date;
end if;
END;
//
DELIMITER //
CREATE TRIGGER trig_2 before update
ON <table_name> FOR EACH ROW
BEGIN
IF new.due_date <>old.due_date then
set new.end_date=new.due_date;
end if;
IF new.end_date <> old.end_date then
set new.due_date=new.end_date;
end if;
END;
//

sql after insert trigger error

I have to check specific keyword's weight and force those which have weight > 3 to 3. It's my very first time I work with triggers, this is my code but it doesn't work, why?
CREATE DEFINER = CURRENT_USER TRIGGER `elections`.`keyword_AFTER_INSERT`
AFTER INSERT ON `keyword`
FOR EACH ROW
BEGIN
if(weight > 3) then begin
set weight = 3;
end;
end if;
END
Try this, where the delimiter has been put. Several changes has been made and I'm not sure what you mean with 'elections':
DELIMITER ;;
CREATE DEFINER = CURRENT_USER TRIGGER `keyword_AFTER_INSERT`
BEFORE INSERT ON `keyword`
FOR EACH ROW
BEGIN
IF NEW.weight > 3 then
set NEW.weight = 3;
END IF;
END;

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 ;

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 ;

MYSQL an after insert trigger to change a fields value

I have two tables stock and order and i am trying to get the trigger to work so that after the an order has been put in the order quantity is then taken away from the stock quantity field.
DELIMITER $$
CREATE TRIGGER stock_update
AFTER INSERT ON order
FOR EACH ROW
BEGIN
SET #quantity = NEW.quantity
UPDATE stock s;
SET s.quantity = OLD.quantity - NEW.quantity
FROM stock s, order o
WHERE s.ID_stock = o.ID_stock;
END;
$$
DELIMITER;
but i am just getting the error number 1064
There are several issues:
order is a reserved word, therefore you need to use back ticks around it
you don't need to use a variable #quantity
update statement was wrong
DELIMITER and ; should be separated with a space
Try this
DELIMITER $$
CREATE TRIGGER stock_update
AFTER INSERT ON `order`
FOR EACH ROW
BEGIN
UPDATE stock s
SET s.quantity = s.quantity - NEW.quantity
WHERE s.ID_stock = NEW.ID_stock;
END $$
DELIMITER ;
I'm not quite sure it will work, since I would need the table definitions in order to test it.
In each case you should remove the FROM clause and also the last line DELIMITER;. You
should also end the line SET #quantity = NEW.quantity whith a semicolon.
DELIMITER $$
CREATE TRIGGER stock_update
AFTER INSERT ON order FOR EACH ROW
BEGIN
UPDATE stock s;
SET s.quantity = OLD.quantity - NEW.quantity
WHERE s.ID_stock = NEW.ID_stock;
END;
$$