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

Slides:



Advertisements
Similar presentations
Organisation Of Data (1) Database Theory
Advertisements

Tools & Processes for Managing Price Updates in Global Edge Presented by: Derek Kratz.
Programming Logic and Design, Third Edition Comprehensive
Database Implementation Issues CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 5 Slides adapted from those used by Jennifer Welch.
Database Design Concepts Info 1408 Lecture 2 An Introduction to Data Storage.
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
Database Design Concepts Info 1408 Lecture 2 An Introduction to Data Storage.
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
MIS 301 Information Systems in Organizations Dave Salisbury ( )
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
FILES AND DATABASES. A FILE is a collection of records with similar characteristics, e.g: A Sales Ledger Stock Records A Price List Customer Records Files.
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
Why do we need a database?
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
CPSC 252 Hashing Page 1 Hashing We have already seen that we can search for a key item in an array using either linear or binary search. It would be better.
1 CSCD 326 Data Structures I Hashing. 2 Hashing Background Goal: provide a constant time complexity method of searching for stored data The best traditional.
Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.
Virtual Memory Pranav Shah CS147 - Sin Min Lee. Concept of Virtual Memory Purpose of Virtual Memory - to use hard disk as an extension of RAM. Personal.
1 of 5 This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS DOCUMENT. © 2007 Microsoft Corporation.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Storage and File Organization
Stack and Heap Memory Stack resident variables include:
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Memory Management.
Chapter 2 Memory and process management
Course Developer/Writer: A. J. Ikuomola
Module 11: File Structure
Databases Key Revision Points.
Chapter 14: Protection Modified by Dr. Neerja Mhaskar for CS 3SH3.
CHP - 9 File Structures.
Paging COMP 755.
Containers and Lists CIS 40 – Introduction to Programming in Python
LEARNING OBJECTIVES O(1), O(N) and O(LogN) access times. Hashing:
CIS 155 Table Relationship
Chapter 11: File System Implementation
Data Structures Interview / VIVA Questions and Answers
Stack Data Structure, Reverse Polish Notation, Homework 7
Database Implementation Issues
Chapter 11: File System Implementation
Disk Storage, Basic File Structures, and Hashing
Database Implementation Issues
Hash Tables.
Chapter 11: File System Implementation
Programming Logic and Design Fourth Edition, Comprehensive
Data Structures and Algorithms
Introduction to Database Systems
Relational Database Model
Java Programming Arrays
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Advance Database System
Flat Files & Relational Databases
One-Pass Algorithms for Database Operations (15.2)
DATABASE IMPLEMENTATION ISSUES
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Please use speaker notes for additional information!
Spreadsheets, Modelling & Databases
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Data Structures Unsorted Arrays
Chapter 11: File System Implementation
Database Implementation Issues
Data Structures and Algorithm Analysis Hashing
Testing & Security Dr. X.
Database Implementation Issues
Database Implementation Issues
Chapter 5: Hashing Hash Tables
Lecture-Hashing.
Presentation transcript:

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

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 address as a natural key. The advantage would be that users can be expected to remember their address when they login, so they don't have to remember any other identifier. The address is a natural key because it is assumed unique, but has other purposes within the system, e.g. for sending account passwords.

Surrogate keys Sometimes a surrogate (made-up or artificial) key is preferred, because no suitable natural unique key exists. The address would be unsuitable if 2 accounts could share the same 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.

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.

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.

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].

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.

Dynamic allocation example

Dynamic allocation example 2

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.

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

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

Deleting records 3

Deleting records 4

Deleting records 5

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.

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.