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

  • Home
  • SEARCH
  • 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 9232419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:18:37+00:00 2026-06-18T06:18:37+00:00

Lets say I want to store users and groups in a MySQL database. They

  • 0

Lets say I want to store users and groups in a MySQL database. They have a relation n:m. To keep track of all changes each table has an audit table user_journal, group_journal and user_group_journal. MySQL triggers copy the current record to the journal table on each INSERT or UPDATE (DELETES are not supported, because I would need the information which application user has deleted the record–so there is a flag active that will be set to 0 instead of a deletion).

My question/problem is: Assuming I am adding 10 users into a group at once. When I’m later clicking through the history of that group in the user interface of the application I want to see the adding of those 10 users as one step and not as 10 independent steps. Is there a good solution to group such changes together? Maybe it is possible to have a counter that is incremented each time the trigger is … triggered? I have never worked with triggers.

The best solution would be to put together all changes made within a transaction. So when the user updates the name of the group and adds 10 users in one step (one form controller call) this would be one step in the history. Maybe it is possible to define a random hash or increment a global counter each time a transaction is started and access this value in the trigger?

I don’t want to make the table design more complex than having one journal table for each “real” table. I don’t want to add a transaction hash into each database table (meaning the “real” tables, not the audit tables–there it would be okay of course). Also I would like to have a solution in the database–not in the application.

  • 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-18T06:18:38+00:00Added an answer on June 18, 2026 at 6:18 am

    I played a bit around and now I found a very good solution:

    The Database setup

    # First of all I create the database and the basic table:
    
    DROP DATABASE `mytest`;
    CREATE DATABASE `mytest`;
    USE `mytest`;
    CREATE TABLE `test` (
        `id` INT PRIMARY KEY AUTO_INCREMENT,
        `something` VARCHAR(255) NOT NULL
    );
    
    # Then I add an audit table to the database:
    
    CREATE TABLE `audit_trail_test` (
        `_id` INT PRIMARY KEY AUTO_INCREMENT,
        `_revision_id` VARCHAR(255) NOT NULL,
        `id` INT NOT NULL,
        `something` VARCHAR(255) NOT NULL
    );
    
    # I added a field _revision_id to it. This is 
    # the ID that groups together all changes a
    # user made within a request of that web
    # application (written in PHP). So we need a
    # third table to store the time and the user
    # that made the changes of that revision:
    
    CREATE TABLE `audit_trail_revisions` (
        `id` INT PRIMARY KEY AUTO_INCREMENT,
        `user_id` INT NOT NULL,
        `time` DATETIME NOT NULL
    );
    
    # Now we need a procedure that creates a
    # record in the revisions table each time an
    # insert or update trigger will be called.
    
    DELIMITER $$
    
    CREATE PROCEDURE create_revision_record()
    BEGIN
        IF @revision_id IS NULL THEN
            INSERT INTO `audit_trail_revisions`
                (user_id, `time`)
                    VALUES
                (@user_id, @time);
            SET @revision_id = LAST_INSERT_ID();
        END IF;
    END;
    
    # It checks if a user defined variable
    # @revision_id is set and if not it creates
    # the row and stores the generated ID (auto
    # increment) into that variable.
    # 
    # Next I wrote the two triggers:
    
    CREATE TRIGGER `test_insert` AFTER INSERT ON `test` 
        FOR EACH ROW BEGIN
            CALL create_revision_record();
            INSERT INTO `audit_trail_test`
                (
                    id,
                    something,
                    _revision_id
                ) 
            VALUES
                (
                    NEW.id,
                    NEW.something,
                    @revision_id
                );
        END;
    $$
    
    CREATE TRIGGER `test_update` AFTER UPDATE ON `test` 
        FOR EACH ROW BEGIN
            CALL create_revision_record();
            INSERT INTO `audit_trail_test`
                (
                    id,
                    something,
                    _revision_id
                ) 
            VALUES
                (
                    NEW.id,
                    NEW.something,
                    @revision_id
                );
        END;
    $$
    

    The application code (PHP)

    $iUserId = 42;
    
    $Database = new \mysqli('localhost', 'root', 'root', 'mytest');
    
    if (!$Database->query('SET @user_id = ' . $iUserId . ', @time = NOW()'))
        die($Database->error);
    if (!$Database->query('INSERT INTO `test` VALUES (NULL, "foo")'))
        die($Database->error);
    if (!$Database->query('UPDATE `test` SET `something` = "bar"'))
        die($Database->error);
    
    // To simulate a second request we close the connection,
    // sleep 2 seconds and create a second connection.
    $Database->close();
    sleep(2);
    $Database = new \mysqli('localhost', 'root', 'root', 'mytest');
    
    if (!$Database->query('SET @user_id = ' . $iUserId . ', @time = NOW()'))
        die($Database->error);
    if (!$Database->query('UPDATE `test` SET `something` = "baz"'))
        die($Database->error);
    

    And … the result

    mysql> select * from test;
    +----+-----------+
    | id | something |
    +----+-----------+
    |  1 | baz       |
    +----+-----------+
    1 row in set (0.00 sec)
    
    mysql> select * from audit_trail_test;
    +-----+--------------+----+-----------+
    | _id | _revision_id | id | something |
    +-----+--------------+----+-----------+
    |   1 | 1            |  1 | foo       |
    |   2 | 1            |  1 | bar       |
    |   3 | 2            |  1 | baz       |
    +-----+--------------+----+-----------+
    3 rows in set (0.00 sec)
    
    mysql> select * from audit_trail_revisions;
    +----+---------+---------------------+
    | id | user_id | time                |
    +----+---------+---------------------+
    |  1 |      42 | 2013-02-03 17:13:20 |
    |  2 |      42 | 2013-02-03 17:13:22 |
    +----+---------+---------------------+
    2 rows in set (0.00 sec)
    

    Please let me know if there is a point I missed. I will have to add an action column to the audit tables to be able to record deletions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a fairly simple app that lets users store information on
I want to store a list of objects, lets say of type Car, but
lets say you want to get all the columns of a table, but exclude
I have two OSGi bundles deployed on Apache Karaf container. Lets say they are
Lets say you want to create a listing of widgets The Widget Manufacturers all
Lets say we have photographers who use one computer in a department. They take
lets say i have 5 lines within a txt file called users.txt each line
how should I store user data in asp.net mvc? Let's say a user want
Let's say I want to store a group of function pointers in a List<(*func)>
Let's say I want to design a REST store used to manage a list.

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.