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

The Archive Base Latest Questions

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

I have a master table, say tbl , which has three columns. Say I

  • 0

I have a master table, say tbl, which has three columns. Say I create a table that only shows the first record that is unique with respect to the combinations of col1 and col2. The SQL for such a table is defined as below:

CREATE TABLE pseudo_view AS SELECT col1, col2, col3 FROM tbl GROUP BY col1 col2

Now I see all unique combinations of col1 and col2 in this new table. I update this table such that col3 for a given record is changed. I want this to apply back to the master table tbl by affecting all duplicate records.

I am aware that this is not possible with a standard view. However, is there a way to create a dependent table, that, when it is updated, the master table will be updated as defined by the select mechanism? I desire a solution such that this pseudo-view table can have its records updated and deleted freely while also affecting the parent table.

  • 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-16T20:16:57+00:00Added an answer on June 16, 2026 at 8:16 pm

    It would work with some triggers on the pseudo_view and tbl

    For insert, update and delete on tbl the “view” needs to update accordingly. Normally that is easy. The only hard part is when you delete the last pair of col1 and col2.

    For update and delete on pseudo_table you need to define what should change on the master.

    In my example I’ve choosen to have min(3) as the function so when a row is updated that same row (the one that used to have the smallest col3) is update and a new (possibly the same) value for the psuedotable is calculated.

    When a row is deleted all rows in tbl needs to go as they would otherwise have given a row in pseudo as well (and that was just deleted).

    CREATE TABLE tbl (col1 INT, col2 INT, col3 INT);
    INSERT INTO tbl VALUES (1, 1, 1), (1, 1, 2), (2, 2, 3), (2, 2, 4);
    
    CREATE TABLE pseudo_view
      AS
        SELECT
          col1,
          col2,
          MIN(col3) AS col3
        FROM tbl
        GROUP BY col1, col2;
    
    DELIMITER //
    
    -- update the row that originally was the min() and make sure that the new row in pseudo is the new min()
    CREATE TRIGGER upd_pseudo
    BEFORE UPDATE ON
      pseudo_view FOR EACH ROW
      BEGIN
        UPDATE tbl
        SET col3 = NEW.col3
        WHERE col1 = OLD.col1
              AND col2 = OLD.col2
              AND col3 = OLD.col3;
    
        SET NEW.col3 := (SELECT
                           MIN(col3)
                         FROM tbl
                         WHERE col1 = OLD.col1
                               AND col2 = OLD.col2);
    
      END //
    
    -- delete all rows in tbl that has this col1 and col2
    CREATE TRIGGER del_pseudo
    AFTER DELETE ON
      pseudo_view FOR EACH ROW
      BEGIN
        DELETE FROM tbl
        WHERE col1 = OLD.col1 AND col2 = OLD.col2;
      END //
    
    -- update pseudo to make sure it's still true
    CREATE TRIGGER upd_tbl
    AFTER UPDATE ON
      tbl FOR EACH ROW
      BEGIN
        UPDATE pseudo_view
        SET col3 = (SELECT
                      min(col3)
                    FROM tbl
                    WHERE col1 = NEW.col1 AND col2 = NEW.col2)
        WHERE col1 = NEW.col1 AND col2 = NEW.col2;
      END//
    
    -- update pseudo to make sure it's still true
    CREATE TRIGGER ins_tbl
    AFTER INSERT ON
      tbl FOR EACH ROW
      BEGIN
        REPLACE pseudo_view
          VALUES (NEW.col1, NEW.col2, (SELECT
                                         min(col3)
                                       FROM tbl
                                       WHERE col1 = NEW.col1 AND col2 = NEW.col2));
      END//
    
    -- update pseudo to make sure it's still true
    CREATE TRIGGER del_tbl
    AFTER DELETE ON
      tbl FOR EACH ROW
      BEGIN
        -- todo: add special case when the delete row was the last of that col1/col2-pair
        REPLACE pseudo_view
          VALUES (NEW.col1, NEW.col2, (SELECT
                                         min(col3)
                                       FROM tbl
                                       WHERE col1 = NEW.col1 AND col2 = NEW.col2));
      END//
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table PGM_MASTER, which has three columns PGM_ID | int(11) PGM_ENV_ID |
Say I have a RadGrid that shows info on what a customer has ordered.
Situation: I have two tables, say 'master' and 'detail', where 'master' has two columns
I have a master table that I am inserting data into from an import
I have a MySQL Table that looks like this: The SQL to create the
I have several databases that each have a table called, say, products . One
Say you have a simple table that represents a time series for another entity
This is regarding query in Oracle. I have one master table, which stores id,
I have three tables that contain data relating to a Job. One table is
Say I have two spreadsheets. One of the spreadsheets has a table like this:

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.