views:

39

answers:

3

I have 2 tables Product

Product
--------
ProductID
ProductName

and SalesTransaction.

SalesTransaction
--------
TransactionID
ProductID
ProductName

User's requirement state that "Product's name can change over time but this modification must not have any effect on already exist transaction". I want to know, is my design good for this kind of requirement? If not, how to improve my design?

+2  A: 

If the requirement means that old transactions must keep a record of the product's name at the time of the transaction then, yes, this is one way to achieve that.

Another way is to just model a product's name change with a new productId. You'd need an additional field in your Product table to mark historical records. When the name of a product changes just duplicate the product's record with a new product ID and new name, and mark the old record as historical.

Andrew Cooper
A: 

You could also split Product into two tables such as Product and ProductDetail where ProductDetail stores things that can change like the name and price. ProductDetail also has From and To date columns that indicate when it was active. After a record has been placed in ProductDetail the only column that can change is the To date, everything else is immutable. When the product name changes you insert a new ProductDetail and set the To date on the old one.

In the SalesTransaction table you can then join to ProductDetail instead of Product and you will always get the right product name.

Andrew Kennan
+1  A: 

That is the traditional method. This is not a denormalization but rather a saving of required information from a point in time. Don;t foregt to do the same thing with the prices of the items inteh order. Some people don't think do this (which you must do for these types of records whether the requirement specifically states it or not) and create a world of hurt for themselves when they start running financial reports from orders because the price attached to the order is no longer the price the customer was actually charged.

HLGEM
I face problem, I can't track product name with this method. I think it may has another method that more elegant and robust than this one.
In The Pink
Why can't you track the product name, you are storing it in the Sales Transaction table? Please stop trying for "elegance", "elegance" is worthless and is often actively harmful to database oprations. The most effective method is often what an application programmer would consider as awkward. It is a BAD thing in database terms.
HLGEM