i have a table named reservation which consists of column reservationStatus and place and another table spot which consists of spot_id and spot_status.
i have created a trigger in reservation table which should activate after the reservationStatus has been set to 0. but it is not working
DELIMITER $$
CREATE TRIGGER spotCancel
AFTER UPDATE ON reservation
FOR EACH ROW
BEGIN
IF NEW.reservationStatus = 0 AND OLD.place <=> 'AbcMall' THEN
UPDATE spot SET spot_status = 0 WHERE spot_id <=> OLD.spot_id;
END IF;
IF NEW.reservationStatus = 0 AND OLD.place <=> 'XyzGym' THEN
UPDATE spot1 SET spot_status =0 WHERE spot_id <=> OLD.spot_id;
END IF;
END$$
DELIMITER ;
This query fails silently everytime and i couldnot find the error. I am using xampp and mysql innoDB storage engine.
You're missing an END IF.
DELIMITER $$
CREATE TRIGGER spotCancel
AFTER UPDATE ON reservation
FOR EACH ROW
BEGIN
IF NEW.reservationStatus = 0 AND OLD.place <=> 'Abc' THEN
UPDATE spot SET spot_status = 0 WHERE spot_id <=> OLD.spot_id;
END IF;
IF NEW.reservationStatus = 0 AND OLD.place <=> 'Def' THEN
UPDATE spot1 SET spot_status =0 WHERE spot_id <=> OLD.spot_id;
END IF;
END$$
DELIMITER ;
Related
CREATE TRIGGER UPDATE_CAR_DETALS
AFTER UPDATE ON BOOKING_DETALS
FOR EACH ROW
WHEN (IFNULL(TO_CHAR(NEW.ACT_RET_DT_TIME),NULL) <> 'NULL' OR
NEW.BOOKING_STATUS ='C')
Multi statements in triggers need a BEGIN and END
Also when you use query tabs you need also DELIMITER
CASE WHEN you would use if you have multiple choices, but you use better IF THEN
DELIMITER $$
CREATE TRIGGER UPDATE_CAR_DETALS
AFTER UPDATE ON BOOKING_DETALS
FOR EACH ROW
BEGIN
IF (IFNULL(TO_CHAR(NEW.ACT_RET_DT_TIME),NULL) <> 'NULL' OR NEW.BOOKING_STATUS ='C') then
BEGIN
IF NEW.BOOKING_STATUS ='C' THEN
UPDATE CAR SET AVAILABILITY_FLAG = 'A' , LOC_ID = NEW.PICKUP_LOC
WHERE REGISTRATION_NUMBER = NEW.REG_NUM;
ELSE
IF IFNULL(TO_CHAR(NEW.ACT_RET_DT_TIME),NULL) <> NULL THEN
UPDATE CAR SET AVAILABILITY_FLAG = 'A' , LOC_ID = NEW.DROP_LOC, MILEAGE = MILEAGE+GET_MILEAGE
WHERE REGISTRATION_NUMBER = NEW.REG_NUM;
END IF;
END IF;
END;
END IF;
END;
DELIMITER ;
Would like to create a trigger in mysql need to
update t2.orderstatus as 'inactive' on update of t2.inactivedate and
update t2.orderstatus as 'active' on update of t2.activedate. Tried to adapt below one. but failed
DROP TRIGGER IF EXISTS trigger_on_stockhistory_update;
DELIMITER $$
CREATE TRIGGER trigger_on_stockhistory_update
AFTER update ON stockhistory.inactivedate
FOR EACH ROW
BEGIN
UPDATE mastersku
SET ordernow ='InActive'
WHERE `SSKU` = NEW.SSKU;
END;
$$
DELIMITER;
You can not set trigger on field change. It may be only on table level:
DROP TRIGGER IF EXISTS trigger_on_stockhistory_update;
DELIMITER $$
CREATE
TRIGGER `trigger_on_stockhistory_update` AFTER UPDATE
ON `stockhistory`
FOR EACH ROW BEGIN
IF OLD.inactivedate <> NEW.inactivedate THEN
UPDATE mastersku
SET ordernow ='InActive'
WHERE `SSKU` = NEW.SSKU;
END IF;
IF OLD.activedate <> NEW.activedate THEN
UPDATE mastersku
SET ordernow ='Active'
WHERE `SSKU` = NEW.SSKU;
END IF;
END$$
DELIMITER ;
Or a little elegant query using CASE statement:
DROP TRIGGER IF EXISTS trigger_on_stockhistory_update;
DELIMITER $$
CREATE
TRIGGER `trigger_on_stockhistory_update` AFTER UPDATE
ON `stockhistory`
FOR EACH ROW BEGIN
UPDATE mastersku
SET ordernow = CASE
WHEN OLD.inactivedate <> NEW.inactivedate THEN 'InActive'
WHEN OLD.activedate <> NEW.activedate THEN 'Active'
ELSE ordernow
END
WHERE
SSKU = NEW.SSKU;
END$$
DELIMITER ;
I am writing a trigger on mysql table. The code is following
DELIMITER $$
CREATE TRIGGER update_on_product_option after update on db_name.table_name
FOR each row
BEGIN
if new.isactive <> old.isactive then
SET #inc=1;
-- update statement on some table here
else if new.lastupdatedts <> old.lastupdatedts then
-- update statement on some table here
set #t = 0;
end if;
end if;
END;
$$
DELIMITER ;
As you can see there are two end if at the end and surprisingly this trigger function is not throwing any errors even though there is an extra end if; at the end. If I remove one end if, then it throws an error. unable to understand why this is happening.
Instead of ELSE IF, MySQL's syntax uses ELSEIF (without the space).
https://dev.mysql.com/doc/refman/5.7/en/if.html
DELIMITER $$
CREATE TRIGGER update_on_product_option after update on db_name.table_name
FOR each row
BEGIN
if new.isactive <> old.isactive then
SET #inc=1;
-- update statement on some table here
elseif new.lastupdatedts <> old.lastupdatedts then
-- update statement on some table here
set #t = 0;
end if;
END;
$$
DELIMITER ;
Although you might be able to make it work with the space in ELSE IF by adding an additional END IF. By using the space, you effectively initiate a second IF statement, which must be closed independently of the first outer IF statement.
This is completely normal(nested two IF stamements so both IF have to be closed):
if new.isactive <> old.isactive then
SET #inc=1;
else if new.lastupdatedts <> old.lastupdatedts then
set #t = 0;
end if;
end if;
You probably wanted:
if new.isactive <> old.isactive then
SET #inc=1;
elseif new.lastupdatedts <> old.lastupdatedts then
SET #t = 0;
end if;
The difference is ELSE IF vs ELSEIF.
Use elseif (without space ) instead of else if. The reasion is when you use else if with space its consider else is use with starting if and you create new condition with if so its required to close with end if. So in your query required two end if.
DELIMITER $$ CREATE TRIGGER update_on_product_option after update on db_name.table_name FOR each row BEGIN if new.isactive <> old.isactive then SET #inc=1; -- update statement on some table here elseif new.lastupdatedts <> old.lastupdatedts then -- update statement on some table here set #t = 0; end if; END; $$ DELIMITER ;
This
CREATE TRIGGER pubdate BEFORE UPDATE ON `table1` FOR EACH ROW SET NEW.date=NOW()
on any column update in table would SET date=NOW().
How to add exception when updating columns id and name?
Update: basically run TRIGGER pubdate on update any table column except id and/or name.
Mysql doesn't support column-based trigger actions, so your intention can't be coded at the action level of the trigger.
You can simulate the action in the logic of the rigger though by checking if any if the other columns have changed.
Assuming there are 3 other columns col1, col2, col3:
DELIMITER $$
CREATE TRIGGER pubdate BEFORE UPDATE ON `table`
FOR EACH ROW BEGIN
IF NOT (NEW.col1 <=> OLD.col1
AND NEW.col2 <=> OLD.col2 -- compare more other column as required
AND NEW.col3 <=> OLD.col3) THEN
SET NEW.date = NOW();
END IF;
END$$
DELIMITER ;
FYI the <=> operator is mysql's null-safe equals, which considers null <=> null to be true.
If trigger is to be triggered when the id/name have not been touched:
DELIMITER $$
CREATE TRIGGER pubdate BEFORE UPDATE ON `table1`
FOR EACH ROW BEGIN
IF (NEW.id = OLD.id and NEW.name = OLD.name) THEN -- Condition for updating
SET NEW.date = NOW();
END IF;
END$$
DELIMITER ;
[Updated]
If the trigger is to be triggered when columns other than id/name have been touched (should be triggered when any_other_column and id/name are touched) as well:
DELIMITER $$
CREATE TRIGGER pubdate BEFORE UPDATE ON `table1`
FOR EACH ROW BEGIN
IF (NEW.email <> OLD.email or NEW.other_column1 <> OLD.other_column1 ... ) THEN
-- Do this for all other columns in the table (except for id and name)
SET NEW.date = NOW();
END IF;
END$$
DELIMITER ;
I'm trying to write an update trigger that will only update a password when a new password is set in the update statement but I'm having a terrible time trying to nail down the syntax. This should be a no-brainer but I'm just not finding the solution.
Here's my code:
CREATE TRIGGER upd_user BEFORE UPDATE ON `user`
FOR EACH ROW BEGIN
IF (NEW.password <> '') THEN
SET NEW.password = PASSWORD(NEW.password);
END IF;
END;
I've tried:
IF (NEW.password <> NULL) THEN
IF (NEW.password) THEN
IF NEW.password <> NULL THEN
IF (NEW.password > 0) THEN
IF (NEW.password != NULL) THEN
And I'm sure many other combinations but it's just not working. Does anyone have any insight?
I think you mean to update it back to the OLD password, when the NEW one is not supplied.
DROP TRIGGER IF EXISTS upd_user;
DELIMITER $$
CREATE TRIGGER upd_user BEFORE UPDATE ON `user`
FOR EACH ROW BEGIN
IF (NEW.password IS NULL OR NEW.password = '') THEN
SET NEW.password = OLD.password;
ELSE
SET NEW.password = Password(NEW.Password);
END IF;
END$$
DELIMITER ;
However, this means a user can never blank out a password.
If the password field (already encrypted) is being sent back in the update to mySQL, then it will not be null or blank, and MySQL will attempt to redo the Password() function on it. To detect this, use this code instead
DELIMITER $$
CREATE TRIGGER upd_user BEFORE UPDATE ON `user`
FOR EACH ROW BEGIN
IF (NEW.password IS NULL OR NEW.password = '' OR NEW.password = OLD.password) THEN
SET NEW.password = OLD.password;
ELSE
SET NEW.password = Password(NEW.Password);
END IF;
END$$
DELIMITER ;
Try to do...
DELIMITER $$
CREATE TRIGGER aumentarsalario
BEFORE INSERT
ON empregados
FOR EACH ROW
BEGIN
if (NEW.SALARIO < 900) THEN
set NEW.SALARIO = NEW.SALARIO + (NEW.SALARIO * 0.1);
END IF;
END $$
DELIMITER ;