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 7799959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:28:40+00:00 2026-06-02T00:28:40+00:00

I’m trying to create some triggers for my MySQL tables to keep track of

  • 0

I’m trying to create some triggers for my MySQL tables to keep track of changes. I thought of a table like

CREATE TABLE IF NOT EXISTS `contacts_changes` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `action` enum('insert','update','delete') NOT NULL,
  `contact_id` int(6) NOT NULL,
  `changes` text NOT NULL,
  PRIMARY KEY (`id`)
);

where contact_id is a foreign key to the table I want to monitor and in column changes I want to store the changes made using the JSON format.

So if I change the familyname and birthday of an entry in the contact table the changes column should contain {"familyname": "Smith", "birthday": "1982-06-24}.

I found a lot of examples for using triggers to track changes but none of them summarizes the changes made in a single column of a single row. What they do is to insert a new row for each change made, i.e. one row for changing the birthday and another for changing the familyname.

Due to my lack of knowledge of the (My)SQL language I failed to figure out if it’s possible to do something I had in mind and if it is how it can be done.

Any ideas or hints how to solve this?

Thanks!

Edit: Posted former edit as answer.

  • 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-02T00:28:41+00:00Added an answer on June 2, 2026 at 12:28 am

    Alright, solved it myself. Wasn’t that hard … 😉

    DELIMITER ;;
    CREATE TRIGGER `contacts_bu` BEFORE UPDATE ON `contacts` FOR EACH ROW
    BEGIN
    SET @json = "{";
    SET @first = true;
    IF (OLD.name!=NEW.name) THEN
        SET @first = false;
        SET @json = CONCAT(@json, "\"name\"", ":", "\"", NEW.name, "\"");
    END IF;
    IF (OLD.birthdate!=NEW.birthdate) THEN
        IF (!@first) THEN
           SET @json = CONCAT(@json, ",");
        END IF;
        SET @first = false;
        SET @json = CONCAT(@json, "\"birthdate\"", ":", "\"", NEW.birthdate, "\"");
    END IF;
    
    SET @json = CONCAT(@json, "}");
    
    INSERT INTO contacts_changes (`action`, `contact_id`, `changes`) 
    VALUES ('update', NEW.id, @json);
    
    END;;
    DELIMITER ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace

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.