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 ;
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 ;
I have a database where whenever residential address update in user table I want it to store in history table of user. For that I'm trying to write triggers but failing miserably in phpmyadmin. Also it's not giving me proper reason why so I can correct it. This is what I have done so far.
DROP TRIGGER IF EXISTS `record_history`;
CREATE TRIGGER `record_history` AFTER UPDATE ON `s_user`
FOR EACH ROW
BEGIN
DECLARE date_current datetime;
DECLARE residential_address varchar(1000);
SET #date_current = NOW();
SET #residential_address = NEW.residential_address;
IF (#residential_address <> OLD.residential_address AND #residential_address != "" AND #residential_address IS NOT NULL) THEN
INSERT INTO history_residential_address (`s_u_id`, `residential_address`, `status`, `date_added`, `date_updated`) VALUES
(OLD.s_u_id, #residential_address, 1, #date_current, #date_current);
END IF;
END;
delimiter ;
A cleaner version of your code
DROP TRIGGER IF EXISTS `record_history`;
delimiter $$
CREATE TRIGGER `record_history` AFTER UPDATE ON `s_user`
FOR EACH ROW
BEGIN
IF (new.residential_address <> OLD.residential_address AND new.residential_address <> "" AND new.residential_address IS NOT NULL) THEN
INSERT INTO history_residential_address (`s_u_id`, `residential_address`, `status`, `date_added`, `date_updated`) VALUES
(OLD.s_u_id, new.residential_address, 1, now(), now());
END IF;
END $$
delimiter ;
If you are still having problems please add sample data from s_user as text to the question.
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;
//
DELIMITER //
CREATE TRIGGER never_update_with_null
BEFORE UPDATE
ON `table_name`
FOR EACH ROW
BEGIN
IF OLD.name IS NOT NULL AND NEW.name IS NULL THEN
SET NEW.name = OLD.name;
IF OLD.phone IS NOT NULL AND NEW.name IS NULL THEN
SET NEW.phone = OLD.phone;
IF OLD.place IS NOT NULL AND NEW.place IS NULL THEN
SET NEW.place = OLD.place;
END; //
DELIMITER ;
I am trying to achieve the following:
If someone UPDATEs a value in name, phone or place(columns) in the table table_name and its value (the updating value) is NULL then the value stays the old one (does not get updated).
Maybe a little shorter: never update with NULL.
The above SQL does not create a trigger but it either does not throw any error(phpMyAdmin). Does it lack syntax? Already tried it with ENDIF; after every OLD.column; - no improvement. Thank you for explanation.
Try
DELIMITER //
CREATE TRIGGER never_update_with_null
BEFORE UPDATE
ON `table_name`
FOR EACH ROW
BEGIN
IF OLD.name IS NOT NULL AND NEW.name IS NULL THEN
SET NEW.name = OLD.name;
END IF;
IF OLD.phone IS NOT NULL AND NEW.phone IS NULL THEN
SET NEW.phone = OLD.phone;
END IF;
IF OLD.place IS NOT NULL AND NEW.place IS NULL THEN
SET NEW.place = OLD.place;
END IF;
END; //
DELIMITER ;
my update trigger
BEGIN
DECLARE s VARCHAR(100);
IF (OLD.authUId <> NEW.authUId) THEN SET s = CONCAT(s,'&authUId=', OLD.authUId); END IF;
IF (OLD.autPId <> NEW.autPId) THEN SET s = CONCAT(s,'&autPId=', OLD.autPId); END IF;
.....
INSERT INTO l_dblog(src,newId,oldValue) VALUE('auth_up',new.authId,s);
END
inserted row oldValue column always null insert, whats my problem ?
I have delete trigger is succes work
my Delete trigger:
BEGIN
DECLARE s VARCHAR(60);
SET s = CONCAT('&authUId=', OLD.authUId,'&autPId=', OLD.autPId,'&authTypeId=', OLD.authTypeId,'&authValue=', OLD.authValue);
INSERT INTO l_dblog(src,newId,oldValue)
VALUE('auth_del',OLD.authId,s);
END
Thank you...
Set default value of variable s as blank in your update query and then check weather trigger is working or not. If not than what error you got after running the trigger.
BEGIN
DECLARE s VARCHAR(100) DEFAULT '';
IF (OLD.authUId <> NEW.authUId) THEN SET s = CONCAT(s,'&authUId=', OLD.authUId); END IF;
IF (OLD.autPId <> NEW.autPId) THEN SET s = CONCAT(s,'&autPId=', OLD.autPId); END IF;
.....
INSERT INTO l_dblog(src,newId,oldValue) VALUE('auth_up',new.authId,s);
END