Trigger to change empty values to NULL - mysql

I am trying to write a trigger where if an incoming value is empty (in other words ''), then insert NULL in the table. I have :
DELIMITER //
CREATE TRIGGER avoid_empty
BEFORE INSERT ON EVALUATION
FOR EACH ROW
BEGIN
IF mark = '' THEN SET NEW.mark = NULL;
END IF;
END;
//
DELIMITER ;
Which executes without errors, but it doesn't do what I need.

Try:
DELIMITER //
CREATE TRIGGER `avoid_empty` BEFORE INSERT ON `EVALUATION`
FOR EACH ROW
BEGIN
IF NEW.`mark` = '' THEN
SET NEW.`mark` := NULL;
END IF;
END//
DELIMITER ;
See db-fiddle.

Related

Trigger+procedure issue

As the title says, I have an issue with the trigger + procedure.
What I'm trying to do it's a procedure + trigger that creates an email with the name and surnames only if when I'm trying to insert a student data w/o an email.
Basically where am stuck is, the code makes the fusion of name and surname and creates the email, but how do I place it before it inserts inside the table?
Best regards,
Engineer
Here's the code I have made:
use alumnesTriggers;
drop procedure if exists ex2;
delimiter //
create procedure ex2 (in nom varchar(50), in cognom1 varchar(50),
in cognom2 varchar(50), in domini varchar(50), out email varchar(50))
begin
set email := concat(nom,cognom1,cognom2,'#',domini,'.com');
#set #email := email;
#UPDATE alumnesEmail as ae SET ae.email = #email WHERE nom = #nom;
end //
delimiter ;
drop trigger if exists ex2_2_trigger;
DELIMITER $$
CREATE TRIGGER ex2_2_trigger before insert on alumnesEmail for each row begin
set #mail := new.email;
#if #mail := null or #mail='' then
set #id := new.id;
set #nom := new.nom;
set #cognom1 := new.cognom1;
set #cognom2 := new.cognom2;
call ex2(#nom,#cognom1,#cognom2,'gmail',#email);
UPDATE alumnesEmail SET new.email = #email;
#end if;
END $$
DELIMITER ;
INSERT INTO `alumnesEmail` (`id`, `nom`, `cognom1`, `cognom2`, `email`)
VALUES (80, 'a', 'a', 'a',null);
select * from alumnesEmail where id = 80;
select #email;
select * from alumnesEmail ;
but how do I place it before it inserts inside the table?
In a before trigger, you just set new.email to the target value. This modifies the value that is about to be written to the database.
Also, the procedure seems superfluous here.
The following code should do just what you want:
delimiter $$
create trigger ex2_2_trigger
before insert on alumnesemail
for each row
begin
if new.email is null or new.email = '' then
set new.email = concat(new.nom, new.cognom1, new.cognom2, '#gmail.com');
end if;
end $$
delimiter ;

MySQL need help setting up trigger

I am trying to set up a TRIGGER to clear empty strings before INSERT. Not rocket science but I can't find the error in my syntax!
Here is the TRIGGER itself
USE `ga_abtest_logging`;
DELIMITER $$
CREATE TRIGGER avoid_empty
BEFORE INSERT ON daily_analytics
FOR EACH ROW
BEGIN
IF profileID = ''
THEN SET profileID = NULL
END IF;
END$$
Here is what workbench is showing:
On hover over the END IF it reads syntax error, unexpected END, expecting ';'
Could I have a problem with the settings on my DB? I have gone through the MySQL docs and I think the trigger looks right! Does anyone see anything obviously wrong?
You should make a few changes:
Use the NEW. prefix when referencing a column value
Add a semi-colon at the end of the line where you set the value
For example:
DELIMITER $$
CREATE TRIGGER tr_b_ins_daily_analytics BEFORE INSERT ON daily_analytics FOR EACH ROW BEGIN
IF (NEW.profileID = '')
THEN
SET NEW.profileID = NULL;
END IF;
END $$
DELIMITER ;
The code below should work.
DELIMITER $$
CREATE TRIGGER avoid_empty
BEFORE INSERT ON daily_analytics
FOR EACH ROW
BEGIN
IF NEW.profileID = ''
THEN SET NEW.profileID = NULL;
END IF;
END;$$
DELIMITER ;

error in trigger creation also how to optimise it

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

Using a stored proceedure in multiple triggers

In addition to the notes table, I am applying this same trigger to around 15 other tables. Not sure, but seems like a stored procedure will eliminate the duplicated code. Is this a good application for a stored procedure? If so, how could it be implemented?
CREATE TRIGGER tg_notes_ins BEFORE INSERT ON notes FOR EACH ROW
BEGIN
IF NEW.created_by_user IS NULL OR NEW.created_by_user = '' THEN
SET NEW.created_by_user = #users_id;
END IF;
IF NEW.modified_by_user IS NULL OR NEW.modified_by_user = '' THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
CREATE TRIGGER tg_notes_upd BEFORE UPDATE ON notes FOR EACH ROW
BEGIN
IF NEW.modified_by_user = OLD.modified_by_user THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
I am not claiming this is how to do it, but it seems to work, and would appreciate any opinions. Thanks
DELIMITER $$
CREATE PROCEDURE `createRecord` ()
BEGIN
IF NEW.created_by_user IS NULL OR NEW.created_by_user = '' THEN
SET NEW.created_by_user = #users_id;
END IF;
IF NEW.modified_by_user IS NULL OR NEW.modified_by_user = '' THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
CREATE PROCEDURE `modifyRecord` ()
BEGIN
IF NEW.modified_by_user = OLD.modified_by_user THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
CREATE TRIGGER tg_notes_upd BEFORE UPDATE ON notes FOR EACH ROW
BEGIN
CALL createRecord();
END$$
CREATE TRIGGER tg_notes_ins BEFORE INSERT ON notes FOR EACH ROW
BEGIN
CALL modifyRecord();
END$$
DELIMITER ;

MySQL trigger if condition exists

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 ;