troubleshooting Question

Question about trigger in MySQL

Avatar of philipjonathan
philipjonathanFlag for New Zealand asked on
8 Comments1 Solution1018 ViewsLast Modified:
Hi experts, this might be a long-winded question, but here it goes:

I'm developing an application with MySQL for it database end. One of the requirement for the app is to have built-in audit trail, which monitors for certain tables. For example, if a record is added to customer table, an audit log is appended into customer_history. Example provided here are for illustration. The credit for this idea goes to this author:

customer table:
id | name
1  | John Doe
2  | Jane Doe

customer_history table:
id | name               | action   | action_by | action_time
1  | John Doh         | insert   | Andy         | 2008-08-04 10:00:00
1  | John Doe         | update | Bob           | 2008-08-04 12:00:00
3  | Jonathan Doe  | delete  | Andy         | 2008-08-04 12:30:00

I've read about MySQL triggers and plan to use it. Btw, I'm using MySQL 5.0.41, which does support triggers. And after considering between using triggers and having the audit trail written at app level, I choose for the former option. The reason is to also log changes that are made directly against the database through Query Analyzer, etc. (Please feel free to voice out your opinions / insights about this design, although this is not the real question  :) )

Now, action and action_time columns are easy to handle. But how do I get the name of the user that makes the changes? I need to use the name of the user that is logged in to the web app, not the database user account. The login name is stored in another table.

Thanks in advance
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros