Reputation: 21
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?
Upvotes: 0
Views: 1618
Reputation: 72656
In MySQL you simply can't use a function as a default value for a column, you can use
the keywords: NULL
, CURRENT_TIMESTAMP
and localtimestamp
, while you can't use functions(except for now()
that is internally represented as a synonym of CURRENT_TIMESTAMP
).
So in your case the way to follow is the trigger ON insert ... You need the additional trigger .
Upvotes: 1