NOS : Certificate in Computer Applications
| Home | Table of Contents |


Up: Online Course Material


LESSON 21

ARRANGING RECORDS OF A DATABASE

21. 1 INTRODUCTION

Organizing a database means arranging individual records so that they appear in a sequence that makes a proper sense and helps you to work with database efficiently. The concept can be appreciated by taking an example of telephone directory. Imagine a situation where names in the directory are not arranged alphabetically. This will create a mess and take much longer time to find a particular name in the directory.

21. 2 OBJECTIVES

At the end of this lesson you would be able to

21. 3 DATABASE ORGANISATION

Information must be organized before you can make much sense of it. The elements of a database structure - field name, type, and size are all intended to organise within each record. There are two ways to organize records in database. One is INDEXING and the other is SORTING. Before sorting a database you make sure that the database you want to sort is current:

21. 3. 1 SORT Command

The SORT command gives us the ability to copy the record and structure of a database to another database. Files can be arranged in either ascending order or descending order by using SORT command. Sort command can be used in multiple fields for data arrangement.

This SORT command creates a new database file. But the new file contains records of the original database file only the order is different. The following examples show the forms of the SORT command. Let us consider a database file BIODATA, whose structure is shown in Fig. 21. 1

RECORD NO. NAME SEX AGE
1 Anant M 25
2 Babloo M 13
3 Manju F 30
4 Sibu M 12
5 Soma F 28

Fig. 21.1

Suppose you want to sort the records in this file in increasing order of the AGE field. You have to give the command at the dot prompt:

.USE BIODATA and press ENTER

. SORT ON fieldname TO new filename. Thus you have to give the command

. SORT ON AGE TO BIO and press ENTER

Here field name is AGE and the sorted file name is BIO.DBF

To see the contents of the sorted file give the following commands:

. USE BIO.DBF

. LIST

The sorted file BIO. DBF looks like as shown in Fig. 21.2.

. use biodata

. sort on age to bio

100% sorted 5 records sorted

. use bio

. list

Record# NAME SEX AGE
4 Sibu m 12
2 Babloo m 13
1 Anant m 25
5 Soma f 28
3 Manju f 30

Fig. 21.2

Here you notice one thing that BIO.DBF is a new file. In this new file, the record numbers given by database is in a new order. But Age field is in sequence, because you have sorted the BIODATA.DBF to increasing order of the AGE field. Suppose you want to sort the file in descending order of the AGE field. Then you have to give the command SORT ON AGE/D TO TEMP.DBF. The new file TEMP looks like as shown in Fig. 21.3

. use biodata

. sort on age/d to temp

100% sorted 5 Records sorted

. use temp

. list

Record# NAME SEX AGE
1 Manju f

30

2 Soma f

28

3 Anant m

25

4 Babloo m

13

5 Sibu m

12

Fig. 21.3

21. 3. 2 INDEX command

Index command can also be used to organise a data file. When you index a database, you ask dBASE to create a separate and smaller file based upon designated fields in the database. The index command does not create a new database file. It creates an index file for the database with a default extension (.NDX). Unlike SORT command however INDEX does not permit the user to arrange the data in descending order. It can also be used to arrange the records in multiple fields. The command structure for index command is

.USE BIODATA

. INDEX ON field name TO file name

For example, if we index the file biodata.dbf according to field age to the file bioage then the records in the file biodata.dbf are arranged in ascending order as given in Fig. 21.4

. use biodata

. index on age to bioage

100% indexed 5 Records indexed

. list

Record# NAME SEX AGE
4 Sibu m

12

2 Babloo m

13

1 Anant m

25

5 Soma f

28

3 Manju f

30

Fig 21.4

 

DIFFERENCE BETWEEN SORT AND INDEX COMMAND

 

Sort Command

Index command

Creates a new database file Does not create a new file
Arranges the records in ascending and descending order Arranges the records only in ascending order
File extension is .DBF File extension is .NDX
Slower than index command Faster than sort command
Renumbers records Does not renumber records

21.3.3 FIND and SEEK Commands

The FIND and SEEK commands both are used in indexed database. FIND operates with character strings, while SEEK operates with either character string or numeric values. If SEEK operates with a character string the string must be enclosed in a single or double quotes or square bracket Let us take some examples

. USE BIODATA

. INDEX ON AGE TO BIOAGE

. USE BIODATA INDEX BIOAGE

. FIND 30

. DISPLAY

The screen will look like this

. use biodata

. index on age to bioage

. use biodata index bioage

. find 30

. display

Record#                       NAME             SEX          AGE

3                                  Manju                  f                30

Fig 21.5

The FIND command will move the record pointer to the first record which matches with find query and the DISPLAY command will display it on the screen.

The SEEK command like FIND command also works only with an index file. When you SEEK command with strings, you must use quotes. Consider the previous example, to SEEK the NAME Manju, you must give the command

. USE BIODATA

. INDEX ON AGE TO BIOAGE

. USE BIODATA INDEX BIOAGE

. SEEK 30

. DISPLAY

The screen will look like this

.use biodata

. index on age to bioage

. use biodata index bioage

. seek 30

. display

Record#                       NAME             SEX          AGE

3                                  Manju                  f                30

Fig 21.6

The main difference between SEEK and FIND commands is that SEEK command will not work with macros but FIND command is used in macros.

21.3.4 Counting Command

The COUNT command helps in finding the number of records in the active database that meet a given condition.

. USE BIODATA

. COUNT FOR AGE> 13

The screen will appear as follows:

. use biodata

. count for age >13

3 records

. count

5 records

Fig 21.7

The COUNT command, without any condition, gives the total number of records in the active database file.

. COUNT

The computer prompts

5 RECORDS

21.3.5 LOCATE Command

The LOCATE command is used to find the records in a database which satisfy certain conditions. The general form of LOCATE command is

. LOCATE [scope] FOR [condition]

Suppose you give the command

. USE BIODATA

. LOCATE FOR AGE =25

The computer will prompt

RECORD =1

Your screen will appear like this

. use biodata

. locate for age=25

Record = 1

Fig 21.8

For displaying that particular record in a database give the command DISPLAY and if you want to continue the search give the command CONTINUE along with LOCATE command. Your screen will appear like this

. use biodata

. locate for age=30

Record = 3

. continue

End of LOCATE scope

Command Line          <C:> BIODATA                Rec: EOF/5

Enter a dBASE III PLUS command.

Fig. 21.9

 


Top

 

IN-TEXT QUESTIONS 1

  1. What is the difference between SORT and INDEX command?
  2. What is the usefulness of FIND and SEEK commands?
  3. Define the following command with a suitable example

(a) COUNT (b) LOCATE


Top

21. 4 MODIFYING A DATABASE

Modifying a database is almost as easy as creating a database. You begin with an existing structure and want to modify the database by

Let us explain the steps by taking our previous file BIODATA. Begin with opening the file.

. USE BIODATA

. EDIT 5

Bytes remaining: 4000

CURSOR a-lf.gif (847 bytes)  a-r.gif (846 bytes)

Char:       a-lf.gif (847 bytes)  a-r.gif (846 bytes)

Word: Home End

Pan:  ^   a-lf.gif (847 bytes)  a-r.gif (846 bytes) ^

INSERT

Char:        Ins

Field:        ^N

Help:         F1

DELETE

Char: Del

Word: ^Y

Field: ^U

Up a Field: ­       a-up.gif (854 bytes)

Down a Field:    a-dn.gif (853 bytes)

Exit/Save:           ^End

Abort:                   Esc

NAME:      Soma

SEX:          f

AGE:          28

 

EDIT                          ¦<C:>¦ BIODATA ¦                Rec: 5/5

 

Fig. 21.10

 

After making the desired changes, pressing Ctrl+W saves the changes and exits to the prompt.

(a)  The Record Pointer

How did the EDIT command display only the 5th record? This can be explained by the concept of a record pointer that is maintained for each file opened by dBASEIII+. When the file is open the pointer points to the first record in the file and moves to the other records after a command has been given. For example, with the LIST command, the record pointer moves to the end of the file, since it moves successively from the first record to the last record. The current record (i.e., the record on which the pointer is positioned currently) can be displayed by giving the command. DISPLAY. This command is used to position a particular record, which is to be displayed.

. DISPLAY

DISPLAY [ALL!RECORD<recordno. >]

[<expr_list>] [FOR<expl.>] [TO PRINT]

where:

<expr_list> is a list fields or expressions.

<expl.> is a condition.

.USE BIODATA

.DISPLAY

dBASE will display only the first record along with header which we have discussed earlier.

Let us consider the following example

dBASE will display the 4th record in the file along with header.

 

. use biodata

. goto 4

. display

Record#                       NAME             SEX          AGE

4                                  Sibu                   m               12

.

Fig 21.11

 

The GOTO command followed by the record number moves the record pointer to a specified record.

 

. GOTO 5

This command will position the pointer on the 5th record.

. GOTO<expN>

where <expN> is the record number.

 

 . use biodata

. goto 5

. display

 

Record#                       NAME             SEX          AGE

5                                  Soma                  f                28

 

Fig 21.12

(b) EDIT Command

Similarly, on giving the command EDIT 5, the record pointer moves to the 5th record and displays it for modification.

To summarise, the steps involved in modifying a database, using the EDIT command are:

 

This command positions the record pointer at the required record and displays it for modification

EDIT [ALL![RECORD]<record no. >]

[FIELDS<fieldlist>] [FOR<expl>]

Where:

<fieldlist> is a list of fields to be edited

<expL. > is a condition.

Now we open BIODATA by the command USE

.USE BIODATA

. EDIT

The screen looks like this

CURSOR a-lf.gif (847 bytes)  a-r.gif (846 bytes)

Char:       a-lf.gif (847 bytes)  a-r.gif (846 bytes)

Word: Home End

Pan:  ^   a-lf.gif (847 bytes)  a-r.gif (846 bytes) ^

INSERT

Char:        Ins

Field:        ^N

Help:         F1

DELETE

Char: Del

Word: ^Y

Field: ^U

Up a Field: ­       a-up.gif (854 bytes)

Down a Field:    a-dn.gif (853 bytes)

Exit/Save:           ^End

Abort:                   Esc

NAME:      Soma

SEX:          f

AGE:          28

 

EDIT                          ¦<C:>¦ BIODATA ¦                Rec: 5/5

 

Fig. 21.13

(c) REPLACE command

The REPLACE command is used to REPLACE the contents of one or more specified field either the current record or all record, depending on the form of the command.

 

. REPLACE ALL

Syntax:

REPLACE [ALL] <field> WITH <expr>

[FOR<expl>]

Where:

<field> is the field to be replaced

<expr> is the expression to replace the field

<expl> is a condition.

 

(d) BROWSE Command

BROWSE command gives an option of adding new records. The BROWSE command also provides the option of displaying and modifying selective fields. Once you make changes through BROWSE command you can save the changes and exit to the dot prompt by pressing Ctrl + W. Consider the following example:

. USE BIODATA

. BROWSE

Immediately dBASE will display a screenful of records as shown in Fig. 21.14

The difference between BROWSE and EDIT Command is that several records are viewed simultaneously using BROWSE, while one record is displayed at a time when EDIT is used.

 

CURSOR a-lf.gif (847 bytes)  a-r.gif (846 bytes)

Char:       a-lf.gif (847 bytes)  a-r.gif (846 bytes)

Word: Home End

Pan:  ^   a-lf.gif (847 bytes)  a-r.gif (846 bytes) ^

INSERT

Char:        Ins

Field:        ^N

Help:         F1

DELETE

Char: Del

Word: ^Y

Field: ^U

Up a Field: ­       a-up.gif (854 bytes)

Down a Field:    a-dn.gif (853 bytes)

Exit/Save:           ^End

Abort:                   Esc

NAME      SEX       AGE

Anant       m               25

Babloo           m               13

Manju        f                30

Sibu                m               12

Soma       f                28

 

BROWSE                 <C:>    BIODATA                                 Rec: 5/5

View and edit fields.

Fig. 21.14

 

21. 5 MODIFY STRUCTURE

The MODIFY STRUCTURE command helps to change the structure of an active database file. The command:

. MODIFY STRUCTURE

displays the structure of the database file as shown in figure 21.15 . After adding the field to the file save the modified structure by pressing <Ctrl>+<W>. The MODIFY STRUCTURE command also allows deleting fields or changing the field types and widths. You should keep in mind that if the field widths are reduced, the data in that field may be lost. Hence changes to existing fields must be made with proper care.

Bytes remaining: 3987

CURSOR a-lf.gif (847 bytes)  a-r.gif (846 bytes)

Char:       a-lf.gif (847 bytes)  a-r.gif (846 bytes)

Word: Home End

Pan:  ^   a-lf.gif (847 bytes)  a-r.gif (846 bytes) ^

INSERT

Char:        Ins

Field:        ^N

Help:         F1

DELETE

Char: Del

Word: ^Y

Field: ^U

Up a Field: ­       a-up.gif (854 bytes)

Down a Field:    a-dn.gif (853 bytes)

Exit/Save:           ^End

Abort:                   Esc

Filed Name      

Type     

Width

Dec    

Filed Name     

Type   

Width    

Dec

1 NAME

Character

10

2 SEX

Character

1

3 AGE

Numeric

2

 

MODIFY STRUCTURE           |<C:>|        BIODATA           |Field: 1/3 | |

Enter the field name.

Field names begin with a letter and may contain letters, digits and underscores

Fig. 21.15

21. 6 MORE ON QUERIES

Instead of giving the List to Print command with each and every query, the printer can be activated to print the command as well as the output of the command. The command that can be given to activate the printer is as follows:

. SET PRINT ON

. LIST

To deactivate the printer the command is:

. SET PRINT OFF

21.6.1 Deleting Records

The delete command marks the current record for deletion. When the records are marked for deletion they are not transferred during copy and sort operation. Now we will discuss how delete command is used in different ways.

 

. DELETE RECORD 5 <marks record 5 for deletion>.

1 records deleted

The DELETE command also provides the following options:

OPTION DESCRIPTION
DELETE ALL Mark all records for deletion

DELETE FOR

Mark selective records for deletion

DELETE FILE ABC.FRM

Delete file ABC.FRM on logged disk.

DELETE NEXT 4

Mark next 4 records for deletion.

 Now delete all the records in the active database file BIODATA. You will find an asterisk (*) against the records marked for deletion.

. use biodata

. delete all

5 records deleted

. list

Record#

NAME

SEX

AGE

1

*Anant

m

25

2

*Babloo

m

13

3

*Manju

f

30

4

*Sibu

m

12

5

*Soma

f

28

.

Command Line  ¦<C:>¦      BIODATA        ¦Rec: EOF/5 ¦ ¦

Type a command (or ASSIST) and press the ENTER key .

Enter a dBASE III PLUS command.

Fig. 21.16

In this example we delete the next four records from the active database file.

. use biodata

. delete next 4

4 records deleted

. list

Record#

NAME

SEX

AGE

1

*Anant

m

25

2

*Babloo

m

13

3

*Manju

f

30

4

*Sibu

m

12

5

Soma

f

28

Command Line    ¦<C:>¦        BIODATA   ¦Rec: EOF/5 ¦ ¦

Type a command (or ASSIST) and press the ENTER key .

Enter a dBASE III PLUS command.

Fig. 21.17

(b)  PACK Command

This command removes records permanently from the database file. Once packed a record cannot be recovered. Deleting records in dBASEIII+ is a two step process. The records are first marked for deletion with the DELETE command, and then removed permanently from the file by PACK command

Let us take the following example

. USE BIODATA

. DELETE 3

. PACK

Next if you give LIST command then you will not find record 3 in database.

(c) RECALL Command

The RECALL command is used to unmark those records that were marked for deletion with DELETE command. Like DELETE command, RECALL has several forms.

OPTION

DESCRIPTION

. RECALL RECORD 4

Unmark record 4 for deletion

. RECALL NEXT 3

Unmark the next 3 records for deletion

. RECALL ALL

Unmark all records within the database for deletion.

 

Now we recall all the records marked for deletion in the active database file BIODATA. Notice that the RECALL command unmarks the records, which were marked for deletion earlier.

. use biodata

. recall all

5 records recalled

. list

Record#

NAME

SEX

AGE

1

Anant

m

25

2

Babloo

m

13

3

Manju

f

30

4

Sibu

m

12

5

Soma

f

28

.

Command Line    ¦<C:>¦ BIODATA          ¦Rec: EOF/5 ¦ ¦

Type a command (or ASSIST) and press the ENTER key .

Enter a dBASE III PLUS command.

Fig. 21.18

 

21. 7 CLOSING AN OPEN DATABASE FILE

The USE commands without file name closes an open database file. Open database can also be closed with the QUIT, CLEAR ALL, CLOSE DATABASE and CLOSE ALL commands.


Top

 

IN -TEXT QUESTIONS 2

1.    Differentiate between EDIT and BROWSE commands.

2.    Bring out the uses of the following commands

DELETE

PACK

RECALL

REPLACE

USE

3. Choose the correct answer

(a) Delete command

(i) marks the record for deletion

(ii) append the record

(iii) change the number

(iv) none of the above

(b) To add record at the end of the database, you would use

(i) continue

(ii) list

(iii) append

(iv) use

(c) Which of the following command physically remove the records

(i) change

(ii) modify

(iii) pack

(iv) none of the above

(d) Which of the following command is used to close a database file.

(i) stop

(ii) use

(iii) start

(iv) none of the above

(e) The modify structure command helps

(i) to change the structure of an active database file.

(ii) to remove the structure

(iii) to name the structure

(iv) none of the above

 


Top

21.8 WHAT YOU HAVE LEARNT

In this lesson we discussed the procedures of organising a database. In this process we distinguished between SORT and INDEX commands, FIND and SEEK commands, DELETE and PACK commands, BROWSE and EDIT commands. Now you should be in a position to count the number of records in a database and modify structure of a database.

21.9 TERMINAL QUESTIONS

1. What are the different methods of data organisation.? Explain briefly.

2.  Write short notes on SEEK, FIND, GOTO, EDIT, RECALL and PACK commands.

3. Define SORT and INDEX command with suitable example.

4. What are the different steps involved in modifying a database, while using EDIT command?

5. Define COUNT command with example.

 

21.10 FEEDBACK TO IN-TEXT QUESTIONS

IN-TEXT QUESTIONS 1

1.

Files can be arranged in either ascending order or descending order by using SORT command. But incase of INDEX command you can arrange the file only ascending order. Index command does not create a new database file. But SORT command creates a new database file.

2.

FIND operates with character strings, while SEEK operates with either character string or numeric values.

3.

(a) The COUNT command helps in finding the number of records in the active database that meet a given condition. Example:

. USE NAME

. COUNT FOR AGE> 11

(b) The LOCATE command is used to find the records in a database which satisfy certain conditions. Example:

. USE BIODATA

. LOCATE FOR AGE=18

 

IN-TEXT QUESTIONS 2

  1. EDIT command displays one a particular record that is the record at the cursor position. But BROWSE command displays all the records in the database file.
  2. DELETE: marks the record for deletion.

PACK: Permanently deletes the records from the database file, which were earlier marked for deletion by DELETE command.

RECALL: Unmarks the record marked earlier.

REPLACE: used to REPLACE the contents of one or more specified field

USE: Closes the active database file.

3.

(a) i

(b) iii

(c) iii

(d) ii

(e) i


Top