oct 2001 cylaufoxpro chapter 71 chapter 7 multiple tables

28
Oct 2001 cylau Foxpro Chapter 7 1 Chapter 7 Multiple Tables

Upload: brittany-chapman

Post on 03-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 1

Chapter 7

Multiple Tables

Page 2: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 2

Two Tables FoxPro Chap. 7 Q.10

STAFF(staff_id, name, shop_id)

01 Chan TM mk

02 Wong ML cb03 Leung DD wts04 Chow YC mk05 Fung YO cb

SHOP(shop_id, location)

cb Causeway Bay

mk Mong Kokwts Wong Tai Sin

Page 3: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 3

Work Area

If 2 or more tables are to be opened, 2 or more work areas are needed

1 table uses 1 work areawork area are named 1, 2, 3, ...

Page 4: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 4

Work Area 1

USE staff && in work area 1USE shop && staff will be closed, shop

opened in work area 1LIST && records of shop are

listed, but not those of staffUSE staff && shop will be closed, staff

opened in work area 1LIST && records of staff are

listed, but not those of shopONE work area cannot hold TWO files

Page 5: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 5

SELECT a Working AreaSELECT 1USE staff && staff is stored in area 1SELECT 2USE shop && shop is stored in area 2LIST && records of shop are listedSELECT 1LIST && records of staff are listed

TWO files, ONE in each of the TWO working areas

Page 6: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 6

Alias

Alias is used to refer to a work area using a meaningful or shorter name instead of numbers

If alias is not provided by the user, the name of the opened table becomes the alias for that work area

Page 7: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 7

Alias

SELECT 1USE staff alias sta && default is alias staffSELECT 2USE shop alias sh && default is alias shopSELECT sta && c.f. SELECT 1SELECT sh && c.f. SELECT 2

It is difficult to remember which table is in working area 1 and which is in 2

Page 8: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 8

Access Data from Another Work AreaBy using SELECT, we don’t need to close a file

when we open anotherTwo files can be opened at the same timeHowever, only ONE table is currently selectedUse qualified field names to access

? Name, shop_id, sh->locationChan TM mk Causeway Bay

What’s Wrong? Does mk mean Causeway Bay?

Page 9: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 9

Not Linked

STAFF(staff_id, name, shop_id) && currently selected

01 Chan TM mkSHOP(shop_id, location) && not currently

selected

cb Causeway Bay

? Name, shop_id, sh->location

Chan TM mk Causeway Bay

? Name, shop_id, sh->shop_id, sh->location

Chan TM mk cb Causeway Bay

Page 10: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 10

SET RELATION TO … INTO ...

SELECT shINDEX ON shop_id TO shopSELECT staSET RELATION TO shop_id INTO sh

?Name, shop_id, sh->locationChan TM mk Mong Kokskip?Name, shop_id, sh->locationWong ML cb Causeway Bay

Page 11: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 11

Parent Table - Currently selected

STAFF(staff_id, name, shop_id)

01 Chan TM mk

02 Wong ML cb03 Leung DD wts04 Chow YC mk05 Fung YO cb

Note the TWO entries of mk and cb (many)STAFF and SHOP have common field shop_id

Page 12: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 12

Child Table - not currently selected but linked to

SHOP(shop_id, location)

cb Causeway Bay

mk Mong Kokwts Wong Tai Sin

Note the uniqueness of cb, mk and wts (one)Child table should be indexed according to

the common key expression (shop_id)Common key expression is usually the

Primary Key of the child table

Page 13: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 13

Points to Note Staff ---------> Shop

Parent ---------> Child

Currently Selected ---------> not currently selected

foreign Key = (usually) Primary Key

MANY to ONEMay not be sorted Sorted(indexed)

SELECT Parent SET RELATION TO common key expression INTO child If key expression in child is not unique, given 1st matching record

Page 14: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 14

Common Errors

ONE ----> MANY, child NOT indexed (Wrong)USE oneSET RELATION TO key INTO many

Cyclic Relation (not allowed)TableA ---> TableBTableB ---> TableA

Page 15: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 15

Correct Relations

MANY parentONE child, indexed on key

This is correct (MANY ---> ONE, ONE indexed on key)USE oneINDEX ON key TO oneUSE many

SET RELATION TO key INTO one

Page 16: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 16

After SET RELATION...STAFF(staff_id, name, shop_id)

fptr --> 01 Chan TM mk

02 Wong ML cb03 Leung DD wts04 Chow YC mk05 Fung YO cb

SHOP(shop_id, location)

cb Causeway Bay

fptr --> mk Mong Kokwts Wong Tai Sin

Page 17: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 17

SKIPSTAFF(staff_id, name, shop_id)

01 Chan TM mk

fptr --> 02 Wong ML cb03 Leung DD wts04 Chow YC mk05 Fung YO cb

SHOP(shop_id, location)

fptr --> cb Causeway Bay

mk Mong Kokwts Wong Tai Sin

Page 18: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 18

Other Ways to Open Tables (1)

SELECT 0 && lowest unused number=1USE staffSELECT 0 && lowest unused number = 2USE shopSELECT staff && work area 1SELECT shop && work area 2

Note the default alias names for work area 1 and 2

Page 19: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 19

Other Ways to Open Tables (2)

USE staff in 0 && staff in work area 1USE shop in 0 && shop in work area 2Note: currently selected table is staff

SELECT 1USE staffSELECT 2USE shopcurrently selected table is shop

Page 20: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 20

Store RelationshipsUsing VIEW FilesThe View Files (with extension .VUE)

enables us to save the current environment settings, all opened tables and any relationships between them

CREATE VIEW infofilenameTo restore the environmentSET VIEW TO infofilenameThe infofilename works like a Data

Dictionary

Page 21: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 21

Data Dictionary

A data dictionary contains information (meta-data) about the tables and their relationships

Meta-data = data about dataTo check or change the information about

related files in a Data DictionarySET VIEW TO infofilenameSET VIEW ON

Page 22: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 22

COPY STRUCTURE EXTENDED TO ...

Manually create field descriptionsUSE staffCOPY STRUCTURE EXTENDED TO a:\staDictUSE a:\staDict…Create table from a data dictionaryCREATE newstaff FROM a:\staDict

Page 23: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 23

UPDATE

Update one table from anotherRequires 2 tables open at the same

timeThe 2 tables should have a common

key field, both indexed

UPDATE ON key FROM fileA ;REPLACE field1 WITH eExpr && fileB->field2

Page 24: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 24

Limitations of UPDATE

Linked by key fields, not Key expressions

Inflexible, ‘FOR’ cannot be usedCan handle only TWO tables at one

time

SET RELATION TO + REPLACE can do the same job

Page 25: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 25

Three Tables

STUDENT(class, no, name)7A 01 Chan7A 02 Lee7A 03 Cheung7S 01 Wong7S 02 Ho

7S 03 Au SUBJECT(code, sub_name)

ca Computer Applicationsms Maths and Stathis Historyphy Physics

Page 26: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 26

ONE <-- MANY ---> ONE

STUD_SUBJ(class, no, code)7A 01 ca7A 01 ms7A 02 his7A 03 ca7S 01 ms7S 02 phy7S 03 ca7S 03 ms

STUDENT <-- STUD_SUBJ --> SUBJECT

Page 27: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 27

How to Set Relations? Select 1 Use student alias stu Index on class+no to student (first time) Select 2 Use subject alias sub Index on code to subject (first time) Select 3 Use stud_subj alias ss Set relation to class+no into stu, code into sub (one line!)

&& Set relation to class+no into stu&& Set relation to code into sub (two lines not ok!)

List fields class, no, stu->name, sub->sub_name

Page 28: Oct 2001 cylauFoxpro Chapter 71 Chapter 7 Multiple Tables

Oct 2001 cylau Foxpro Chapter 7 28

Always from many to one Select 1 Use student alias stu && one 7A 01 Set index to student Select 2 Use subject alias sub && one ca Set index to subject Select 3 Use stud_subj alias ss && many 7A 01, many ca Set relation to class+no into stu, code into sub (one line!)

&& Set relation to class+no into stu&& Set relation to code into sub (two lines not ok!)

List fields class, no, stu->name, sub->sub_name