39
Visual FoxPro Ch5 by CHAN G YU 1 Chapter Five Index and Search • The concept of index. • Create , open and close index file. • Type of index files. • Set the master index. • Locate, seek and find. • Filter the record of tables.

Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Embed Size (px)

Citation preview

Page 1: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

1

Chapter FiveIndex and Search

• The concept of index.

• Create , open and close index file.

• Type of index files.

• Set the master index.

• Locate, seek and find.

• Filter the record of tables.

Page 2: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

2

Concept

name Records are ordered by name.

Page 3: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

3

Concept

Records are ordered by pay.

Page 4: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

4

Concepts• The original table is ordered by entry number.• It is difficult to find a record in a big table.• It is easy and quick to search a record if we use index file.• A table is allowed to have many index files. Each index

file has an index key and the table will be ordered by index key value. If you want the table to be displayed in an order by some index, you must set a master index key.

• Many commands will operate based on the new order by the master index when the index file is created. E.g. List, Display, Skip, Go top, Go Bottom.

Page 5: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

5

The type of index file

• Type of index files:– Standard Single-Entry Index File.– Compact Single-Entry Index File.– Structural Compound Index File.– Non-Structural Compound Index File.

• We will introduce how to create, open , close an index file and set a master index.

Page 6: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

6

Standard Single-Entry Index FileCreate an index file

• Index on <Expression> To <IDXFileName>

Note: 1. Open the table first. 2. Create an index file. 3. Index key becomes the master index at once. 4. Records are ordered by master index. 5. Table (*.dbf) remains as before but is displayed by master index only.

Index key– is a column name

or expression of columns.Index file name (*.idx)

Page 7: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

7

Standard Single-Entry Index File

E.g . Use person1

index on name to name.idx

browse

display status

use

clear

dir *.idx

Close table and index file.

Index file exists already.

Page 8: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

8

Standard Single-Entry Index FileOpen an index file

• Use < TableName> [ Index <IDXFilelist> ] The command will open the table and its index file together.

– use person1 index name, pay

• Set index to <IDXFileList>– Use person1 Set index to name, pay

• Set order to < n > | < IDXFilename > Convert to another master index.

– Use person1 index name, pay, birth Set order to 2 ( = set order to pay) display status

Name.idx is master index.

Page 9: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

9

Standard Single-Entry Index File

• Index on <Expression> To <IDXFileName>

– Open database staff1 use person1 index on departno To departno browse index on departno+name To depname Browse last display status

is a column name or column expression .

the type of departno and name must be matched.

Major sort key

Minor sort key

Page 10: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

10

Standard Single-Entry Index File

• STR ( <nExpression>[ , <nlength> [, <nDecimalPlace> ] ] )

This function will convert numeric type to character .

• DTOC( ) will convert the “date” to “character”.

Numeric expressionLength of character

Decimal number

Page 11: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

11

Standard Single-Entry Index File

• use person1 index on departno + str(pay, 6) to deppay browse ? Str(1234.567,6,1) ? Str (1234.567, 6)• use person1 index on departno+DTOC(birthday) to depbirth browse

Page 12: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

12

Standard Single-Entry Index FileReindex

• If we delete, modify and insert in a table without opening its index file, we should use the “Reindex” command to rearrange them.

• use person 1 index pay

browse replace pay with 13000 browse last use use person1 delete record 14 pack browse last set index to pay browse last reindex

Error message

Page 13: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

13

Standard Single-Entry Index FileClose an index file

• Close index

• Set index to

Note: Those commands will close the index file. But the table is still open.

Page 14: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

14

Compact Single-Entry Index File

• Index on name To name Compact

This command will create a compact single-entry index file. (*.idx)– index on tel to tel compact

display status

Page 15: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

15

Structural Compound Index File

• This kind of index files (*.cdx) consist of many compact single-entry index files (*.idx).

name.idx pay.idx

tel.idx birth.idx departno.idx departpay.idx

Person1.cdx

Compact single-entry index file.

Index tag

Page 16: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

16

Structural Compound Index File

• The advantage of using structural compound index files:– easy to maintain.– easy to open. Using the [use] command to

open the table and its index file together.

Page 17: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

17

Structural Compound Index FileCreate

1. Index on <Expression> Tag <TagName> [ Ascending | Descending ]

– use person1 index on no Tag no display status clear browse

1. Create a person1.cdx file.

2. Add a no tag index in the .cdx file.

3. “No” becomes the master index at once.

4. Display records order by ascending or descending number.

Page 18: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

18

Structural Compound Index FileCreate

2. Create a .cdx file in the table designer.– Open table call the table designer press the page tab Order: descending or ascending.

Name: name of index tag.

Expression: expression of index key.

Type : regular index, unique index, primary index and candidate index.

Page 19: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

19

Structural Compound Index FileCreate

• Type of tag indices:– Regular Index: – Unique Index:– Primary Index:– Candidate Index:

Page 20: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

20

Non-Structural Compound Index File

• SCIF and NSCIF– Many index tags can be saved in two kinds of .cdx files.– The name of SCIF must be same as the table name. But t

he name of NSCIF must not be same as the table name.– The SCIF(*.cdx) file will be opened with the table togeth

er. But the NSCIF .cdx file cannot be opened with the table by default.

– A table can have only one SCIF .cdx file, but can have many NSCIF .cdx files.

Page 21: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

21

Non-Structural Compound Index File

• Index on <Expression> Tag <TagName>

Of <CDXFileName> [ Ascending | Descending ]– use person1

index on tel Tag tel of personns

display statusTel is the master index.

Page 22: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

22

Index File (*.cdx )

• Index on <Expression> To <IDXFileName> | Tag <TagName>

[ Of <cdxfileName> ] [ compact ] [ Ascending | Descending ]

Standard Single-Entry Index File(*.idx)

Compact Single-Entry Index File(*.idx)

Structural Compound Index File(*.cdx)

Non-structural Compact Index File(*.cdx)

Page 23: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

23

Index File

Standard Single-Entry Index File.

Compact Single-Entry Index File.

Index on <expression> To <Idxfilename>

Index on <expression> To <Idxfilename> compact

*.idx file

Page 24: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

24

Index File

*.cdx file:

Structural Compound Index File

Non-Structural Compound Index File

Index on <expression> Tag <Tagname> [Ascending | Descending ]

Index on <expression> Tag <Tagname> Of <cdxfilename>

Page 25: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

25

Using Index Filesopen

• Open the structural index file: – Use person1 It opens the table “person1.dbf” and “person1.cdx”. But it does not set any master index.

• Open the standard single-entry index file , compact single-entry index file and non-structural compound index file : – Use person1 index name, birth, personns– Display status

Open person1.cdx, personns.cdx, name.idx and birth.idx . “name” is the master index.

Page 26: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

26

Using Index FilesSet the master index

• Open *.cdx file : does not set any master index.– Use person1 index personns display status

• Open *.idx file : the file which is at the first position is the master index.– use person1 index name, personns, birth display status

“name” is the master index.

Page 27: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

27

Using Index FilesSet the master index

• Open *.cdx and *.idx at a same time.– use person1 index name, personns, birth

Display status

– use person1 index personns, name, birth

display status

Does not set any master index.

“name” is the master index.

Page 28: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

28

Using Index Filesconvert the master index

– use person1 index name, birth, personns

set order to 2

browse

set order to 6 ?

display status

set order to name of personns

display status

set order to no descending

Page 29: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

29

Set order to Use person1 index name, birth, personns

Set order to 2

Use person1

Set index to name, birth, personns

Set order to 2

Use person1

Set index to name, birth, personns order 2

Use person1 index name, birth, personns order 2

=

=

=

Page 30: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

30

Close the Index File

• USE , CLOSE ALL, CLOSE DATABASE.

• Set index to• Close index

Close *.idx and NSCIF *.cdx only.

Page 31: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

31

Search

• Locate: It can search data not only directly in the original table, but also using an index file (in a quicker way).

• Seek : must search using an index file. • Find : must search using an index file.• Set filter to go top / browse: filter the records in the

table.

Page 32: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

32

Locate

• Locate [ For <IExpression> ] [ <scope> ]

– Search data without opening any index file: The pointer will go to the top first, and then find the data one by one (a slow process).

– Search data with an index file: A very quick process to find the data. (No need to set any master index)

Go top Filter condition Range

Page 33: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

33

Locate– use person display status clear locate for name = “Tommy” browse locate for pay = 12000 all browse last continue ?Found( ) locate for birthday={^1969.04.29} and sex=.f. browse last

If we want to find the next data with the same condition.

Page 34: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

34

Seek

• Seek <Expression>

– We should create a *.cdx file or a *.idx file and set the master index.

– <Expression> is any type of expression.– <Expression> can include some variable.– Seek cannot find data whose type is general or

memo .

Page 35: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

35

Seek• Use person set order to name seek “Tommy” browse seek “Mary” browse last set order to pay seek 12000 browse last x = 2000 seek 12000 + x browse last

Page 36: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

36

Seek

• use person order birth seek {^1960.02.11} browse index on name +SRT(pay,6) to namepay compact display status seek “tommy” + str(36000,6) browse last skip ? Found( )

Page 37: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

37

Find

• Find < Expression>

– Find command cannot find data whose types are memo, general, logical, or date. It can find constant and string only.

– <Expression> does not include any variable.

Page 38: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

38

Find– Use person order name find “Tommy” browse last set order to pay find “12000” browse last x = 2000 find 12000 + x browse last set order to birth find {^1960.02.11} browse last

Error message.

Type match

No variables

Can not search date.

Page 39: Visual FoxPro Ch5 by CHANG YU 1 Chapter Five Index and Search The concept of index. Create, open and close index file. Type of index files. Set the master

Visual FoxPro Ch5 by CHANG YU

39

Set Filter To

Set Filter to [<IExpression>] + Go top | Browse– Open database staff

use person

set filter to sex = .f.

display no,name,sex

browse

set filter to pay > 30000

go top

• This command can operate together with the “browse”, “go top”, and other commands of moving pointer.

• If we want to search quickly, we must create an index tag (no need to set any master index).