Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 index. Locate, seek and find. Filter the record of tables.

2 Visual FoxPro Ch5 by CHANG YU 2 Concept name Records are ordered by name.

3 Visual FoxPro Ch5 by CHANG YU 3 Concept Records are ordered by pay.

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

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

6 Visual FoxPro Ch5 by CHANG YU 6 Standard Single-Entry Index File Create an index file Index on To 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)

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

8 Visual FoxPro Ch5 by CHANG YU 8 Standard Single-Entry Index File Open an index file Use [ Index ] The command will open the table and its index file together. – use person1 index name, pay Set index to –Use person1 Set index to name, pay Set order to | 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.

9 Visual FoxPro Ch5 by CHANG YU 9 Standard Single-Entry Index File Index on To –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

10 Visual FoxPro Ch5 by CHANG YU 10 Standard Single-Entry Index File STR ( [, [, ] ] ) This function will convert numeric type to character. DTOC( ) will convert the “date” to “character”. Numeric expression Length of character Decimal number

11 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

12 Visual FoxPro Ch5 by CHANG YU 12 Standard Single-Entry Index File Reindex 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

13 Visual FoxPro Ch5 by CHANG YU 13 Standard Single-Entry Index File Close an index file Close index Set index to Note: Those commands will close the index file. But the table is still open.

14 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

15 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

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

17 Visual FoxPro Ch5 by CHANG YU 17 Structural Compound Index File Create 1. Index on Tag [ 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.

18 Visual FoxPro Ch5 by CHANG YU 18 Structural Compound Index File Create 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.

19 Visual FoxPro Ch5 by CHANG YU 19 Structural Compound Index File Create Type of tag indices: – Regular Index: – Unique Index: – Primary Index: – Candidate Index:

20 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 the name of NSCIF must not be same as the table name. – The SCIF(*.cdx) file will be opened with the table together. 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.

21 Visual FoxPro Ch5 by CHANG YU 21 Non-Structural Compound Index File Index on Tag Of [ Ascending | Descending ] – use person1 index on tel Tag tel of personns display status Tel is the master index.

22 Visual FoxPro Ch5 by CHANG YU 22 Index File (*.cdx ) Index on To | Tag [ Of ] [ 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)

23 Visual FoxPro Ch5 by CHANG YU 23 Index File Standard Single-Entry Index File. Compact Single-Entry Index File. Index on To Index on To compact *.idx file

24 Visual FoxPro Ch5 by CHANG YU 24 Index File *.cdx file: Structural Compound Index File Non-Structural Compound Index File Index on Tag [Ascending | Descending ] Index on Tag Of

25 Visual FoxPro Ch5 by CHANG YU 25 Using Index Files open 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.

26 Visual FoxPro Ch5 by CHANG YU 26 Using Index Files Set 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.

27 Visual FoxPro Ch5 by CHANG YU 27 Using Index Files Set 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.

28 Visual FoxPro Ch5 by CHANG YU 28 Using Index Files convert 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

29 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 = = =

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

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

32 Visual FoxPro Ch5 by CHANG YU 32 Locate Locate [ For ] [ ] –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 topFilter conditionRange

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

34 Visual FoxPro Ch5 by CHANG YU 34 Seek – We should create a *.cdx file or a *.idx file and set the master index. – is any type of expression. – can include some variable. – Seek cannot find data whose type is general or memo.

35 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

36 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( )

37 Visual FoxPro Ch5 by CHANG YU 37 Find –Find command cannot find data whose types are memo, general, logical, or date. It can find constant and string only. – does not include any variable.

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

39 Visual FoxPro Ch5 by CHANG YU 39 Set Filter To Set Filter to [ ] + 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).


Download ppt "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."

Similar presentations


Ads by Google