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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:06:11+00:00 2026-05-26T19:06:11+00:00

I am learning SQL and DB design for a college class. One assignment that

  • 0

I am learning SQL and DB design for a college class. One assignment that was given to us is to create a table with a derived attribute which is the SUM of some child attributes. For example:

ORDERS
orderID {PK}
/orderTotal    /* derived from SUM of child itemTotals */

ITEMS
itemNo {PK}
orderID {FK}
itemTotal

Now, I am not even sure this is good practice. From some reading I’ve done on the web, derived values should not be stored, but rather calculated by user applications. I can understand that perspective, but in this instance my assignment is to store derived values and more importantly to maintain their integrity via triggers, which are relatively new to me so I am enjoying using them. I’d also imagine in some more complex cases that it really would be worth the saved processing time to store derived values. Here are the safeguards I’ve put in place which are NOT giving me problems:

A trigger which updates parent /orderTotal when new child item is inserted.

A trigger which updates parent /orderTotal when child item is deleted.

A trigger which updates parent /orderTotal when child itemTotal is modified.

However, there is another safeguard I want which I cannot figure out how to accomplish. Since the parent attribute /orderTotal is derived, it should never be manually modified. If somebody does attempt to manually modify it (to an erroneous value which is not actually the correct SUM), I want to either (a) prevent them from doing this or (b) revert it to its old value as soon as they are done.

Which is the better approach, and which is possible (and how)? I am not sure how to accomplish the former, and I tried to accomplish the latter via either a trigger or a constraint, but neither one seemed appropriate. The trigger method kept giving me ORA-04091 error for attempting to mutate the table which fired the trigger. The constraint method, I do not think is appropriate either since I’m not sure how to do such a specific thing inside a constraint check.

I am using Oracle SQL by the way, in SQL Developer.

Thanks!

  • 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-05-26T19:06:12+00:00Added an answer on May 26, 2026 at 7:06 pm

    “Now, I am not even sure thise is good practice.”

    Your intuition is right: this is bad practice. For some reason, a lot of college professors set their students the task of writing poor code; this wouldn’t be so bad if they at least explained that it is bad practice and should never be used in the real world. But then I guess most professors have only a limited grasp on what matters in the real world. sigh.

    Anyhoo, to answer your question. There are two approaches to this. One would be to use a trigger to “correct” i.e. swallow the change. This would be wrong because the user trying to modify the value would probably waste a lot of time trying to discover why their change wasn’t sticking, without realising they were breaking a business rule. So, it’s much better to hurl an exception.

    This example uses Oracle syntax, because I’m guessing that’s what you’re using.

    create or replace trigger order_header_trg
        before insert or update
        on order_header for each row
    begin
        if :new.order_total != :old.order_total
        then
            raise_application_error 
                     ( -20000, 'You are not allowed to modify the value of ORDER_TOTAL');
        end if;
    end;
    

    The only problem with this approach is that it will prevent you inserting rows into ORDER_LINES and then deriving a new total for ORDER_HEADER.

    This is one reason why denormalised totals are Bad Practice.

    The error you’re getting – ORA-04091 – says “mutating table”. This happens when we attempt to write a trigger which selects from the table which owns the trigger. It almost always points to a poor data model, one which is insufficiently normalised. This is obviously the case here.

    Given that you are stuck with the data model, the only workaround is a clunky implementation using multiple triggers and a package. The internet offers various slightly different solutions: here is one.

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

Sidebar

Related Questions

I'm learning about table design in SQL and I'm wonder how to create a
I am learning SQL Server by exercising some problems. In one of that problems
I'm having newsequentialid() learning problems in sql server management studio. Create a table with
I'm learning SQL Server Compact for a program I'm writing that requries a local
I'm trying to CREATE a TABLE with an attribute of type DATE in Microsoft
I have been slowly learning SQL the last few weeks. I've picked up all
I would like to start learning SQL Server 2005 on my own. Can anyone
Possible Duplicate: SQL: What’s the difference between HAVING and WHERE? i am learning sql
The setup: Winform/ASP.NET MVC projects. Learning NHibernate SQL-Server driven apps I work with clients
I just started learning LINQ to SQL, and so far I'm impressed with the

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.