If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > passing admin_id to trigger

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-12-10, 09:49
nzo nzo is offline
Registered User
 
Join Date: Jan 2010
Posts: 16
passing admin_id to trigger

I want to have a trigger on a table, when a cell is updated it takes the current value and stores this in a audit table, for example:

table.data_change
id table primary_key column new_value old_value timestamp


But is it possible to also pass an admin_id/user_id from the application to the database along with the update SQL query?

The main reason for using triggers is to prevent round trips between the db and application servers.

Any suggestions?
Reply With Quote
  #2 (permalink)  
Old 12-12-10, 13:45
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
If you create a trigger on a table with BEFORE UPDATE you can reference the NEW.fieldname and OLD.fieldname to get the value before and after the UPDATE. You can INSERT the OLD.fieldname value to your audit table.

As for the user_id, is this an internal username or the username that you are connecting to the database with? If it is the latter then you can user CURRENT_USER(). If it is the internal identifier then you can always add this as part of the table so that during the UPDATE statement you also include this user_id. You can again reference the NEW.user_id and OLD.user_id in your trigger code.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 12-12-10, 14:12
nzo nzo is offline
Registered User
 
Join Date: Jan 2010
Posts: 16
its an internal username, one that is stored in a user table. (not a mysql user).

Yeh I can see what you mean, e.g having a last_update_admin_id, and then passing the admin_id along with the update SQL.

This will work nicely, cheers.
Reply With Quote
  #4 (permalink)  
Old 12-12-10, 14:35
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
I have put together some sample trigger code based on what you are looking for above. Check out MySQL – Triggers « IT Integrated Business Solutions
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 12-12-10, 18:56
nzo nzo is offline
Registered User
 
Join Date: Jan 2010
Posts: 16
Looks good, thanks for the code.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On