I have this MySQL table:
create table test.customer (
ID INTEGER,
CREATION_TIME DATETIME,
CREATION_USER VARCHAR(50),
CHANGE_TIME TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CHANGE_USER VARCHAR(50),
PRIMARY KEY(ID)
);
I want to automatically set the user & timestamps for logging purpose. I do have one trigger before insert (actually written only for the creation_time):
delimiter #
create trigger test.customer_creation
before insert on test.customer
for each row
begin
set NEW.CREATION_TIME = NOW();
IF (NEW.CREATION_USER is null or NEW.CREATION_USER = '')
THEN set NEW.CREATION_USER = USER();
end IF;
end #
delimiter ;
but I don’t really want a trigger just for the CHANGE_USER. Is there a possibility to set the CHANGE_USER with something like: DEFAULT USER()? or do I have to create a additional trigger?
In MySQL you simply can’t use a function as a default value for a column, you can use
the keywords:
NULL,CURRENT_TIMESTAMPandlocaltimestamp, while you can’t use functions(except fornow()that is internally represented as a synonym ofCURRENT_TIMESTAMP).So in your case the way to follow is the trigger ON insert … You need the additional trigger .