tags:

views:

74

answers:

2

I'm creating a trigger in MySQL to let me know WHO (which mysql user) is doing an update on a specific table.

I know MySQL has a function CURRENT_USER(), but is that going to insert the username at the time the trigger is CREATED, or at the time the trigger is CALLED?

This is my trigger so far. I want to insert the username in the 'content' column.

delimiter |
CREATE TRIGGER update_product_procedure 
BEFORE UPDATE ON product_procedure 
FOR EACH ROW BEGIN 
INSERT INTO trigger_logs SET 
content = 'This is a test', postDate=NOW(); 
END;
|
A: 

What do you expect the NOW() function to show? The time of the update, or the time the trigger was created?

Why don't you just try it?

Regards

blackanchorage
NOW is evaluated when the trigger is called and this doesn't related to the question at all.
Kevin Peno
i was trying to make the person think - don't you think current_user() will be evaluated at the same time as now() is evaluated?
blackanchorage
CURRENT_USER() can change, much like you can su in linux, under many circumstances, which is much different in functionality than NOW().
Kevin Peno
I'm with blackanchorage on this one; the only way I would be able to provide an answer in this case would just be to test it... honestly, would it be so hard? I think you'd find the answer in less than 17 minutes, which is the elapsed time as of the writing of this comment.
Dereleased
+1  A: 

I would have put lots of money on it being the invoker, but from http://bugs.mysql.com/bug.php?id=5861:

SQL standard says that: "A triggered action is always executed under the authorization of the owner of the schema that includes the trigger." This means that in MySQL we should execute trigger body under authorization of user who created trigger and not the one who issued statement which invoked this trigger.

Apologies, I assumed it was an obvious question :-(

Regards

EDIT: user() gives the invoker

blackanchorage
Thanks. No worries. I picked USER(), which I didn't know existed.
nute
Thanks for taking the time to research. I also didn't know about the USER(). Good to know :D
Kevin Peno