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 ;
Related
So i have two tables practice and practice1 where both has an auto increment id and a name and surname columns.
I made an after insert trigger where i have an if statement inside, where i put a condition if name is null one insert should occur and if surname is null then the other one but it did not work for some reason. I am sharing the script i tried.
CREATE DEFINER = CURRENT_USER TRIGGER `id_information`.`practice_after_INSERT1` AFTER INSERT ON `practice` FOR EACH ROW
BEGIN
if (`name` = NULL) then
insert into id_information.practice1(surname) values(new.surname);
else
(`surname` = NULL) then
insert into id_information.practice1(name) values(new.name);
end if;
END$$
delimiter ;
Kindly correct my mistake. id_information is the name of the database.
I think the syntax you want is:
delimiter $$
create definer = current_user trigger `id_information`.`practice_after_insert1`
after insert on `practice` for each row
begin
if new.name is null then
insert into id_information.practice1(surname) values(new.surname);
elseif new.surname is null then
insert into id_information.practice1(name) values(new.name);
end if;
end$$
delimiter ;
Key points:
you need to use pseudo-table new to access the row that was just inserted
the structure of your if statement should be if ... then ... elseif ... end
to check a value against null, use is null rather than = null (which is never true)
I am trying to create a trigger to prevent empty string insert for cname column and values smaller than 7000 and larger than 8000 for empno column by making it null. Here is how I have done it:
delimiter $$
CREATE TRIGGER test1
BEFORE insert
ON clients
FOR EACH row BEGIN
if new.cname = '' THEN
SET new.cname = null;
if new.empno <7000 THEN
SET new.empno = null;
if new.empno>8000 THEN
SET new.empno = null;
end if;
end if;
end if;
end;
The cname column works fine. But the empno will accept anything and I cannot figure why. My table is something like this:
CREATE TABLE clients
(
empno INTEGER NOT NULL DEFAULT 7654
cname VARCHAR(20) NOT NULL
);
You should set each end if after the corresponding if:
delimiter $$
CREATE TRIGGER test1
BEFORE insert
ON clients
FOR EACH row BEGIN
if new.cname = '' THEN
SET new.cname = null;
end if;
if new.empno <7000 THEN
SET new.empno = null;
end if;
if new.empno>8000 THEN
SET new.empno = null;
end if;
end;
Please tell me whats wrong with the code. Not able to fine the bug
DELIMITER $$
CREATE TRIGGER update_status BEFORE Update ON listing_basic_new_updated
FOR EACH ROW
if new.processing_status is not null
then begin
SET new.rep_status = New.processing_status;
end;
elseif new.televeri_status is not null
then begin
SET new.rep_status = New.televeri_status;
end;
elseif new.verification_status is not null
then begin
SET new.rep_status = New.verification_status;
end;
end if;
END$$
DELIMITER ;
I think you're missing a BEGIN that would match up to your END at the end:
DELIMITER $$
CREATE TRIGGER update_status BEFORE Update ON listing_basic_new_updated
FOR EACH ROW
BEGIN
if new.processing_status is not null
then begin
SET new.rep_status = New.processing_status;
end;
elseif new.televeri_status is not null
then begin
SET new.rep_status = New.televeri_status;
end;
elseif new.verification_status is not null
then begin
SET new.rep_status = New.verification_status;
end;
end if;
END$$
DELIMITER ;
I think you might be able to replace all of it with
SET new.rep_status = COALESCE(new.processing_status, new.televeri_status,
new.verification_status, new.rep_status);
COALESCE: "Returns the first non-NULL value in the list, or NULL if there are no non-NULL values."
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
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 ;