Upload
gwsy93
View
37
Download
0
Embed Size (px)
Citation preview
Algorithm #3
CHANGE and ADD transactions; Several transactions per master
record
1Prepared by Perla P. Cosme
Get_Next_MasterGet_Next_TransWhile NOT (master_key==sentinel AND trans_key==sentinel) if (master_key < trans_key) { output master record to new master file Get_Next_master } else if (master_key == trans_key) { make a change in the master record output master record in new master file Get_Next_Trans Get_Next_Master } else { print “no matching record in the master file” Get_Next_Trans }
Recall Algorithm #1
2Prepared by Perla P. Cosme
Get_Next_MasterGet_Next_TransWhile NOT (master_key==sentinel AND trans_key==sentinel) if (master_key < trans_key) { output master record to new master file Get_Next_master } else if (master_key == trans_key) { make a change in the master record // output master record in new master file Get_Next_Trans //Get_Next_Master } else { print “no matching record in the master file” Get_Next_Trans }
Recall Algorithm #2
3Prepared by Perla P. Cosme
Think about this ….
If Algorithm #3 is to include ADD transaction on the existing CHANGE transaction in the Algorithm #2, then when are we allowed to ADD or create a record?
a. Master key < transaction keyb.Master key = transaction keyc. Master key > transaction key
Prepared by Perla P. Cosme 4
Think about this ….
If Algorithm #3 is to include ADD transaction on the existing CHANGE transaction in Algorithm #2, then when are we allowed to ADD or create a record?
a. Master key < transaction keyb.Master key = transaction keyc. Master key > transaction key
Prepared by Perla P. Cosme 5
Why (c)? Justify.
Justification why the Answer to the previous Question cannot be (a)
Let us prove by citing an example:MF: 6 7 8
TF 8 ANote: if master key (7) < transaction key (8), and the
transaction is ADD, you might add a record whose primary key is the same as the one you are about to create/add.
Prepared by Perla P. Cosme 6
Justification why the Answer to the previous Question cannot be (b)
Let us show one typical example:MF: 3 6 11
TF: 3 3C A
Note: if master key (3) is equal to transaction key (3), you will be creating duplicate records in the master file. (The master file should never have duplicates.)
Prepared by Perla P. Cosme 7
Question: How should Algorithm #2 look like now to reflect the error in adding a record when master key = transaction key?
Prepared by Perla P. Cosme 8
Get_Next_MasterGet_Next_TransWhile NOT (master_key==sentinel AND trans_key==sentinel) if (master_key < trans_key) { output master record to new master file Get_Next_master } else if (master_key == trans_key) {Case update_code ‘A’: print “duplicate record” Get_Next_Trans ‘C’: make a change in the master record Get_Next_Trans Default: print “invalid update code” Get_Next_Trans } else /* no matching record in the master file */ { print “no matching record in the master file” Get_Next_Trans }
9Prepared by Perla P. Cosme
Justification why the Answer to the previous Question is (c)
• It is in the third case (i.e., master key > transaction key) where we are allowed to add a new record. Hence, it will be in such section of the algorithm where we are going to concentrate making the modifications to Algorithm #2 to fit to our new requirements – CHANGE and ADD transactions with multiple transactions per master record.
Prepared by Perla P. Cosme 10
Get_Next_MasterGet_Next_TransWhile NOT (master_key==sentinel AND trans_key==sentinel) if (master_key < trans_key) { output master record to new master file Get_Next_master } else if (master_key == trans_key) {Case update_code ‘A’: print “duplicate record” Get_Next_Trans ‘C’: make a change in the master record Get_Next_Trans Default: print “invalid update code” Get_Next_Trans } else { // no matching record in the master file print “no matching record in the master file” Get_Next_Trans }
11Prepared by Perla P. Cosme
This is the Algorithm #3 that explicitly
includes the error-handling routine
when master key is equal to transaction
key
Get_Next_MasterGet_Next_TransWhile NOT (master_key==sentinel AND trans_key==sentinel) if (master_key < trans_key) { output master record to new master file Get_Next_master } else if (master_key == trans_key) {Case update_code ‘A’: print “duplicate record” Get_Next_Trans ‘C’: make a change in the master record Get_Next_Trans Default: print “invalid update code” Get_Next_Trans } else { // no matching record in the master file print “no matching record in the master file” Get_Next_Trans }
12Prepared by Perla P. Cosme
These two (2) lines will still be
maintained but only when the update
code is C.
We shall introduce a function called NOMATCH
to handle the ADD transaction and the
succeeding ADD and/or CHANGE transactions on the same newly created
record.
Get_Next_MasterGet_Next_TransWhile NOT (master_key==sentinel AND trans_key==sentinel) if (master_key < trans_key) { output master record to new master file Get_Next_master } else if (master_key == trans_key) {Case update_code ‘A’: print “duplicate record” Get_Next_Trans ‘C’: make a change in the master record Get_Next_Trans Default: print “invalid update code” Get_Next_Trans } else { // no matching record in the master file NOMATCH }
13Prepared by Perla P. Cosme
Function NOMATCHCase update_code ‘A’: Build new record from transaction record new_key = trans_key Get_Next_Trans while (trans_key != sentinel AND trans_key == new_key) Case update_code ‘A’: print “duplicate add” ‘C’: make change in the newly created record Default: print “invalid update code” Get_Next_Trans output new record to new master file ‘C’: print “no matching master record for transaction key” Get_Next_Trans Default : print “invalid update code” Get_Next_Trans
Prepared by Perla P. Cosme 14
These are the lines that were retained from Algorithm #2.
Let’s simulate the SFO using Algorithm #3
We shall use the same example as what we presented previously.
15Prepared by Perla P. Cosme
The Third AlgorithmCHANGE and ADD transactions;
several transactions per master record
MF 3 6 10 11
TF 3 3 11 11 11 11 18C A A C C A A
16Prepared by Perla P. Cosme
Questions? Comments?
Suggestions? and Open Forum
Prepared by Perla P. Cosme 17
????