Consider the following scenario you posted:
'Here's a real problem that your suggestion produces:
- Say you have two users that want to update the same part, maybe one is receiving goods and one is issuing, at the same time. The current balance is read by both routines, and the new value then written back to the parts table. Regardless of which one writes first, the balance will be wrong because it will not include the other transaction.'
...in more detail but lets make it more complicated by having two users wanting to issue the same parts:
Calculated value Scenario:
1. User 1 looks up a part, say 'pump x'.
2. The database goes to the transaction table which has 10K total transactions.
3. It finds all 500 previous transactions for that part.
4. By adding and subtracting all of those transactions, it determines that there must be 5 pump x's in stock.
5. User 1 has all the fields completed to order the part and he plans to order all 5.
6. At the same time user 2 is at the exact same point and plans to order all 5.
(The database has had to go through steps 2-4 all over again)
7. One of the users
has to hit enter first to process the transactions.
8. Let's say user 2 hit enter first. At that time the database would need to recount the transactions again to make sure there was still 5 in stock. (Steps 2-4)
9. The count comes back good and lets user 2 order the pumps.
10. Now user 1 hits enter and the database has to add and subtract all the transactions again. (Steps 2-4)
11. The count comes back bad (not enough to fill his request), and notifies user 1 that it cannot issue the parts because some one else beat him to the punch.
Note: The database had to do steps 2-4, four times.
Stored value scenario:
1. User 1 looks up a part, say 'pump x'.
2. The database looks up the stock qty in the part table and determines there must be 5 pump x's in stock.
3. User 1 has all the fields completed to order the part and he plans to order all 5.
4. At the same time user 2 is at the exact same point and plans to order all 5.
(The database has had to do step 2 all over again)
5. One of the users
has to hit enter first to process the transaction.
6. Let's say user 2 hit enter first. At that time the database would need to
look up the stock qty in the part table and determines there must be 5 pump x's in stock.
7. The count comes back good and lets user 2 order the pumps.
8. The db updates the stock qty in the parts table and places a snapshot of the current stock qty in the new transaction record.
8. Now user 1 hits enter and the database would need to
look up the stock qty in the part table.
9. The count comes back bad (not enough to fill his request), and notifies user 1 that it cannot issue the parts because user 2 beat him to the punch.
Of course, in either case you could lock a part number once a user has the part number pulled up in a transaction mode screen.
=========
I suppose being an accountant, I always want an audit trail.
From my earlier post:
'Perhaps I wasn't clear. The primary location for the current stock qty should be in the part table. A snapshot of the current stock qty is merely stored in the transaction table to see what the balance was at the time of the transaction. Setting up your data in this manner will allow you to track parts information history much better.
ken
