0

After insert in table_user, I want the filed user_notification and user_recover are null. In this moment, this filed are blank and not null because the form send POST data same blank value.

How to create this trigger?

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
Frankie
  • 490
  • 8
  • 23
  • I think you mean: "After inserting a record in TABLE USER, I want the FIELDS user_notification and user_recover to be set to null values. At the moment, these 2 fields are set to empty strings (ie. blank, not null) because of the from sending empty strings" – rmcsharry Sep 07 '16 at 19:53
  • A trigger is not really the correct way to solve this problem - that trigger will fire EVERY time that a record is saved or updated, affecting database performance (in a bad way!). Change the form so it does NOT pass empty strings OR remove them before saving to the database. – rmcsharry Sep 07 '16 at 19:54

1 Answers1

0

I have find a solution:

    delimiter $$
create trigger nullify_blanks_ins before insert on your_table
for each row begin
    if new.string = '' then
        set new.string = null;
    end if;
end;
$$
create trigger nullify_blanks_upd before update on your_table
for each row begin
    if new.string = '' then
        set new.string = null;
    end if;
end;
$$  
delimiter ;

Ref: Is there a way in MySQL to automatically convert empty string to NULL value?

Thanks for previous Answers.

Community
  • 1
  • 1
Frankie
  • 490
  • 8
  • 23