Presentation is loading. Please wait.

Presentation is loading. Please wait.

Keys and adding, deleting and modifying records in an array ● Record Keys ● Reading and Adding Records ● Partition or Sentinels Marking Space in Use ●

Similar presentations


Presentation on theme: "Keys and adding, deleting and modifying records in an array ● Record Keys ● Reading and Adding Records ● Partition or Sentinels Marking Space in Use ●"— Presentation transcript:

1 Keys and adding, deleting and modifying records in an array ● Record Keys ● Reading and Adding Records ● Partition or Sentinels Marking Space in Use ● Dynamic allocation example ● Deleting records ● Modifying Records

2 Record Keys A key is one or more fields used to identify a record in a database table. For the key to be useful in identifying records this key must be unique. A key can be kept unique if: ● New records aren't added if the key is used for an existing record. ● Key fields can't be changed to match other record keys. A suitable natural key may or may not be available. E.G. System accounts might use the email address as a natural key. The advantage would be that users can be expected to remember their email address when they login, so they don't have to remember any other identifier. The email address is a natural key because it is assumed unique, but has other purposes within the system, e.g. for sending account passwords.

3 Surrogate keys Sometimes a surrogate (made-up or artificial) key is preferred, because no suitable natural unique key exists. The email address would be unsuitable if 2 accounts could share the same email address. It might also be unsuitable if the system design requires account anonymity. Another reason for having a surrogate key would be to minimise key size, in order to speed manual data input required for record identification. Perhaps one of the best known surrogate keys is the UK postcode system. This only provides a unique address record key when combined with other parts of the address, e.g. a house number.

4 Keys and database integrity Many system designs will prevent modification of key fields. If this is needed, a record might be deleted and a new one added. Changing a value which is a foreign key in another table will leave records referring to it unlinked. Multi-table system designs will often require records containing a foreign key to be removed before the record they refer to can be removed.

5 Reading and Adding Records Arrays are often the simplest and most convenient data structure used for maintaining sets of records. Having defined a record we can use an array to store as many records as memory allows. The array can be allocated either at compile time or dynamically. If we use dynamic allocation we can calculate the memory needed by counting the records in the file before allocating the required amount of space. This assumes we: * a. open the file * b. count the number of records in the file * c. allocate the required amount of memory space * d. rewind the file (or close and reopen it) * e. read the records from the file into the space now allocated.

6 Avoiding memory overflow Compile time allocation is simpler but less flexible: the maximum number of records is decided for the program at coding time, most conveniently through a global constant e.g. MAXRECS which may be changed prior to compilation. If we want to add records to an array while the program is operating this may only be accomplished safely, without overflowing the array bounds, if a check is made prior to the record being added to ensure that there is unused space within the array for the additional record. If we allocate the array as having space for MAXRECS e.g. RECORD a[MAXRECS]; we can safely use positions from a[0] to a[MAXRECS-1].

7 Array Partition or Sentinels Marking Used Space If the array size allows for more records than are in use at any given time, 2 approaches are commonly used which enable identification of used and unused array positions: ● If n of MAX records are in use, these occupy positions indexed between 0 and n-1 in the array. Positions n to MAX-1 are unused. n is stored as an integer (e.g. int nrecs), which is incremented when a record is read into the array or added to it, and decremented when a record is deleted. This approach allows use of standard search and sort functions. ● A sentinel value is used for the key field (e.g. -1 when used keys are all positive integers) to indicate unused positions. When adding records the first unused position is used. When deleting records the sentinel value is substituted for the key value. Every access to the array will have to look for sentinel values to distinguish used and unused positions.

8 Dynamic allocation example

9 Dynamic allocation example 2

10 Dynamic allocation example 3 The above program code dynamically allocates space for maxrecs records in the array a[] with maxrecs being set as the number of records found in the input file plus 5, to allow for the user to add up to 5 extra records during the program run. The program keeps track of how many records within the array a[] are used at any given time. The approach coded above is naive, because it does not prevent addition of duplicate keys.

11 Deleting records 1 In the example above, where there are maxrecs positions within the array of which nrecs positions are used, it has been assumed that at any given time all array indices between 0 and nrecs-1 are in use and remaining indices from nrecs to maxrecs-1 are vacant. If we adopt this approach there are 2 methods by which we could delete a record. The first involves finding the record to be deleted and shuffling all higher records down by one in turn starting with the record above the delete position and then taking one away from (decrementing) nrecs. If the record we wish to delete is randomly positioned within the used slots we will have to move nrecs/2 records on average to accomplish this

12 Deleting records 2 The approach above will be required if the array is to be kept in sorted order as it doesn't change the order of the remaining records. However, moving on average nrecs/2 records is inefficient for a very large unsorted array if there is no need to maintain the record order. A faster approach is to to move the top record used to the deletion position and decrement nrecs. This only moves a single record

13 Deleting records 3

14 Deleting records 4

15 Deleting records 5

16 Modifying Records A function which modifies a record should generally not modify a key. If a key needs to be modified, this can be done more safely by using the functions to delete the old and add the new version of the record. A well-designed delete() function will prevent removal of a referent from a foreign key in a multi- table database. Other fields can be modified so long as the same validation constraints are maintained as when a record is added.

17 Functionality common to add, mod and delete record functions The index of the record to be modified must be identified. An add() function must identify that the key does not already exist to prevent duplicates. A del() function must identify the index of the record to be removed. The requirement to search for a key is therefore shared between these 3 modules. To avoid breaking modularity, finding the index corresponding to a key or identifying the absence of a key within the array must be programmed as a separate find() function. The add() and del() functions may need to alter nrecs, the number of records in the used partition of the array. The mod() function will not need to change this value.


Download ppt "Keys and adding, deleting and modifying records in an array ● Record Keys ● Reading and Adding Records ● Partition or Sentinels Marking Space in Use ●"

Similar presentations


Ads by Google