MySQL Forums
Forum List  »  Newbie

Re: How should I initialize a header field from the sum of a detail field?
Posted by: Phillip Ward
Date: October 06, 2023 01:40AM

Quote

So what would be the *right* way to do this?

Not to do it at all.

This total can be calculated ("derived") from other data that you already hold, namely the values in the invoicedetails records. It's similar reasoning to why we store someone's Date of Birth and calculate their Age from it.

Create a View that calculates this total and join queries to that View whenever you need this total. Half a million invoicedetail records won't be any problem at all for your database.

Alternatively, if you really must store this [derived] total, use a Trigger to maintain it whenever any invoicedetail record is add/changed/deleted.

Regards, Phill W.

Options: ReplyQuote


Subject
Written By
Posted
Re: How should I initialize a header field from the sum of a detail field?
October 06, 2023 01:40AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.