Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9004661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:56:45+00:00 2026-06-16T00:56:45+00:00

I currently have two tables as follows: data table +—–+————-+—————+ | uri | field1

  • 0

I currently have two tables as follows:

data table

+-----+-------------+---------------+
| uri | field1      | field2        |
+-----+-------------+---------------+
|   1 | word        | somethingelse |
|   2 | somethinge  | values        |
|   3 | change_this | test          |
+-----+-------------+---------------+

log table

+--------+------+----------+-----------+-------------+---------------------+
| log_id | uri  | field_id | old_value | new_value   | modified_date       |
+--------+------+----------+-----------+-------------+---------------------+
|     14 | 3    | field1   | word32    | change_this | 2012-12-18 13:06:27 |
+--------+------+----------+-----------+-------------+---------------------+

When I update the value of field1 or field2 two, I get an entry in my ‘log’ table as to which field was changed, from what to what, when, and the uri of the record that was updated.

My question is regarding the trigger itself: Is it OK as is, or can this be accomplished more efficiently? I’ve honestly never written triggers before, and have pieced this together based on several snippets found through google. I welcome your thoughts/feedback.

DELIMITER $$
DROP TRIGGER IF EXISTS trig_update $$
CREATE TRIGGER trig_update AFTER UPDATE on data
FOR EACH ROW
BEGIN
DECLARE data_uri VARCHAR(32);
IF (NEW.field1 != OLD.field1) THEN
SET data_uri = (SELECT uri FROM data WHERE field1 = NEW.field1);
INSERT INTO log (uri, field_id, old_value, new_value, modified_date) 
VALUES (data_uri, "field1", OLD.field1, NEW.field1, NOW());
END IF; 
IF (NEW.field2 != OLD.field2) THEN
SET data_uri = (SELECT uri FROM data WHERE field2 = NEW.field2);
INSERT INTO log (uri, field_id, old_value, new_value, modified_date) 
VALUES (data_uri, "field2", OLD.field2, NEW.field2, NOW());
END IF; 
END$$
DELIMITER ;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:56:47+00:00Added an answer on June 16, 2026 at 12:56 am

    You should eliminate redundant queries:

    DELIMITER $$
    DROP TRIGGER IF EXISTS trig_update $$
    CREATE TRIGGER trig_update AFTER UPDATE on data
    FOR EACH ROW
    BEGIN
    IF (NEW.field1 != OLD.field1) THEN
      INSERT INTO log (uri, field_id, old_value, new_value, modified_date) 
      VALUES (NEW.uri, "field1", OLD.field1, NEW.field1, NOW());
    END IF; 
    IF (NEW.field2 != OLD.field2) THEN
      INSERT INTO log (uri, field_id, old_value, new_value, modified_date) 
      VALUES (NEW.uri, "field2", OLD.field2, NEW.field2, NOW());
    END IF; 
    END$$
    DELIMITER ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application in which I'm currently using a two column table in
I have two similar tables table_a and table_b table_a is my current data and
I have two tables: Table 1 has Episode and Code, with Episode as distinct.
Currently I have two tables I have select-all functions on the top left checkboxes,
I have a successful query that currently returns all data except for one table.
I have two tables: a source table and a target table. The target table
I currently have two different models: User and Project . The User model has
I currently have two SQL commands. One retrieves a list of unique IDs from
I currently have two Backbone models: user and project. I would like to have
I currently have two apps: app1/ app2/ templates/ app1.html app2.html In app1.html, I'm including

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.