That was for the crowd that won't touch a table unless it's 4 or 5NF. :)
Seriously, if the business decision is to store a calculated field in a
table, at least create a stored procedure or program to verify that
field and optionally correct it. Too many times I've seen
application-only database logic and had to track down why header totals
never matched detail and correct them, to use Joe's example.
Storing the calculation in a UDF is even better because it adheres to
the DRY or DIO principles (don't repeat yourself; do it once). Then the
procedure that calculates the field can be used to correct it if
necessary.
Loyd Goodbar
Business Systems
BorgWarner Shared Services
662-473-5713
-----Original Message-----
From: midrange-l-bounces@xxxxxxxxxxxx
[mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Joe Pluta
Sent: Monday, September 08, 2008 3:19 PM
To: Midrange Systems Technical Discussion
Subject: Re: Calculated/derived fields
lgoodbar@xxxxxxxxxxxxxx wrote:
No real issues with what you propose. One thing database purists go on
about is "do not store calculated fields in a table". Just as you
describe, keeping itdlr7 correct can be difficult.
Just the phrase "database purist" makes me itch <grin>.
Seriously, though, there are valid cases for storing derived fields. We
called this "strategic denormalization" back in the day. A perfect
example is storing the total amount for an order. Yes, you can
calculate it every time by adding the total amount of every line and the
total tax and total freight, but in an order with hundreds of line
items, that's not exactly a good thing, especially since it's a value
that displayed often and even sorted on.
Not quite the issue here - this is a point-in-time calculation like the
BPCS onhand inventory. However, you have to ask yourself how many times
a given price will be read vs. the number of time the price is changed.
The only rule in database design is that all rules depend on the
business requirement.
Joe
As an Amazon Associate we earn from qualifying purchases.