Vous êtes sur la page 1sur 13

CHAPTER 7

USING RELATIONAL POWERS OF FOXPRO

HOW TO RELATE FILES ?


MANAGING THE ONE-TO MANY RELATIONSHIP

108

Chapter 7: Relational Power of FoxPro

USING RELATIONAL POWERS OF FOXPRO


FoxPro is a Relational Database Management System (RDBMS). You
can link a table to one or more other tables, using a field that each
table has in common to create the linkage. The table from which the
link, or relation, is created is called the parent table. Each table
related to the parent is called a child table.
After you link tables, you can treat them as if they were part of a single
table. You can creates single Browse window, for example, that shows
fields from each of the related tables. You do so by preceding each of
the field names with the alias of the work area in which the table is
open. The alias tells FoxPro from which table to take the data. If you
dont specify an alias, FoxPro assumes that youre referring to the
table in the currently active work area, which is usually the parent.

HOW TO RELATE FILES ?


USING COMMANDS
The relational powers provided by FoxPro are easily implemented
from inside the Command window. You can link databases together
with the SET RELATION command, which links the files through a
matching field.
The Format of the SET RELATION command is :
SET RELATION TO (key -expression) INTO (alias) (ADDITIVE)
The key expression is the common field present in both database. The
alias is usually the name of the other database that the active
database is to be linked to Note that ADDITIVE is an optional clause
needed only when you are setting a relation in more than one file at
a time.
The overall process of linking two database using a common field
involves the following steps:
<
<
<
<

Open the file from which you want to establish the relation in
one work area.
In another work area, open the file you wish to link to the first
file.
Activate an index file based on the field (or expression) that is
common on both files.
Use the SET RELATION command to establish the link.

Once the link has been established, any movement of the record
pointer in the active file (also known as the parent file) will cause
FoxPro to seek the corresponding record in the related file (also
known as the child file). The nature of such a relationship can be seen
in the example shown in the following paragraphs.

Chapter 7 : Relational Power of FoxPro

109

One important requirement of the SET RELATION command is that


you must index the related file on the common field. Enter the
following commands now to create an index file for the STATUS
database (Contains fields Code, Limit for the company and Status of
the company), based on the CODE field:
USE STATUS
INDEX ON CODE TO STATUS1
To work with multiple database files you need to open more than one
database file at a time. You choose the work area with the SELECT
command: for example, entering SELECT 2 at the Command level
chooses work area 2.
Open the COMPANY and STATUS database files by using the following
commands.
CLOSE DATABASE
SELECT 1
USE COMPANY
USE ABC IN 2 INDEX STATUS1
It is now possible to link the files, using the Code field, with the SET
RELATION TO command. The COMPANY database is the active
database, so you will link the STATUS database to the COMPANY
database. Enter
SET RELATION TO CODE INTO STATUS
No changes are immediately visible, but FoxPro has linked the files,
To see the effects, enter the commands.
GO 3
DISPLAY
and you see the third record in the COMPANY database. The record
indicates that a company having the CODE number of 4004 WIPRO
SOAPS. To see just, what is the limit & status of the company is enter
these commands:
SELECT 2
DISPLAY
Now the ABC database (open in work area 2) becomes the active
database. The record pointer will be at record 3 (the record containing
the CODE number 4004- showing that the company in question has
the limit of 85000.00 & status is D.

110

Chapter 7: Relational Power of FoxPro

Fig 7.1 Setting relation between two files


Get back to the COMPANY database with these commands:
SELECT 1
GO 2
DISPLAY
Again, because of the relation, FoxPro will have automatically found
a matching CODE number in the STATUS database. You can verify
this by entering these commands:
SELECT 2
DISPLAY
You can retrieve data in the related file by including the alias name
and pointer (that is the file name followed by a period) along with the
field name. In the expression.
STATUS.LIMIT
The file name STATUS is the alias while LIMIT is the field name. The
Period is used as a pointer. To see how this works try the following
commands:
SELECT 1
LIST CODE,COMPANY,STATUS.STATUS,STATUS.LIMIT
The results are shown here :

Chapter 7 : Relational Power of FoxPro

111

Fig 7.2 Displaying Selected Fields


USING VIEW COMMAND
Open the View window using Window > View and close all open
tables. Select work area 1 and open the COMPANY table. Choose
Setup and Set Order to the COMP1 index. (COMP1.IDX is the indexed
file of COMPANY on the field CODE.)

Fig 7.3 Setting order to Index file in Work area 1


Now select work area 2, open the STATUS table, choose Setup, and
Set order to the STATUS1.IDX. (STATUS1.IDX is the indexed file of
STATUS.DBF on the field CODE).

112

Chapter 7: Relational Power of FoxPro

Fig 7.4 Setting Order to index file in work area 2


You are going to set a relation from the COMPANY table into the
STATUS table. For a relation to be set the subsidiary table (in this
case STATUS) must be indexed on the connecting link. You are going
to link by CODE, the only field that contains information shared by
both tables.
The View window look like that shown below.

Fig 7.5 Using View dialog box


Select the COMPANY table and choose Relations. The View window
now looks like that shown below.

Chapter 7 : Relational Power of FoxPro

113

Fig 7.6 Setting relation using view dialog box


The beginning of the relation is complete with the COMPANY table
now established as the controlling table. Select the subsidiary table,
STATUS, by highlighting the name in the list and pressing Enter or by
clicking. This brings the Expression Builder dialog box.

Fig 7.7 Using Expression Builder for Setting relation


Notice that the Expression Builder already contains the name of the
CODE field. FoxPro used the following two pieces of information to
guess that this field should form the link.

114

Chapter 7: Relational Power of FoxPro

<

Each of the tables being linked contains a CODE field.

<

The subsidiary (or target) table is currently ordered by this


field.

If FoxPro could not determine the proper linking field, you would
enter the expression into the Expression Builder text box, either by
typing CODE or by selecting the field name from the Field Names list.
Now select the OK button to complete the task.
After you return to the View window, the screen resembles as follows:

Fig 7.8 View dialog box after setting the relation


Note the SET RELATION command that FoxPro entered in the Command
window to create the relation.

Fig 7.9 SQL for setting relation


Now look at what you accomplished. Select and then Browse the
STATUS table. Everything looks normal. Resize and move the table
out of the way, but where you can still see it. Now select and then
Browse the COMPANY table. The STATUS Browse window has changed
to show only the records that match the current company CODE in
the COMPANY table Browse window. Select a new record in the

Chapter 7 : Relational Power of FoxPro

115

COMPANY table and watch the STATUS table Browse Change to show
a different group of matching records.

Fig 7.10 Browse result after setting relation between two windows
Notice that the above relation created in many-to-one Relation, i.e.
the parent (COMPANY) can have many record that uses a particular
company CODE.

MANAGING THE ONE-TO MANY RELATIONSHIP


As discussed in the preceding sections, you can use SET RELATION
to link two tables, and the tables move together in relationship. As
you move through the Controlling (or parent) file, the first record that
matches in the subsidiary (or child) file is accessed. If more than one
matching record exists, the other records arent available from the
current work area, even though you can see them in their Browse
window.
The parent and child metaphor is useful because it implies that
although each parent might have many children, each child has only
one parent.
In FoxPro you modify the nature of the relationship by using the SET
SKIP command, which enables you to access all the child records
while remaining in the parent work area.
In the case of one-to-many relationship, each record in the controlling
table is joined to all the matching records in the subsidiary table to
create the virtual records, consisting of the information in the
controlling record and information from all the child records that
match the controlling record. When the Browse window is created, a
report, or other representation of a database, you see as many entries
for one parent record as that record has corresponding records in the
child table.

116

Chapter 7: Relational Power of FoxPro

Now return to the View window, with the COMAPNY & STATUS table
open and a relation set on the CODE field between the COMPANY
table & STATUS table. Make sure that the COMPANY table is the
currently selected table.
Notice that after you select the COMPANY table, in the View window,
the 1-to-Many push button is active. Select this option now by
pressing T, or by clicking the button with the mouse. This brings the
1-To-Many dialog box.

Fig 7.11 Setting 1-To-Many relationship


You see only one entry in the Child Aliases list because you established
a relation only between the COMPANY table & STATUS table. You can
also establish relations between the COMPANY table and additional
table. If you establish these relations with other tables, the new
aliases appear in the list. For now, you want to establish one-to-many
relationship between COMPANY & STATUS. Choose All button to
move STATUS table into Selected Aliases list and then select OK to
complete the work. This whole procedure is equivalent to the following
command in the command window, which also appears in the command
window.
SET SKIP TO STATUS

Fig 7.12 SQL statement for 1-to-many relationship


To observe the effects of a one to many relationship, you must issue
a few instructions in the command window.

Chapter 7 : Relational Power of FoxPro

117

Make sure that COMAPNY is the currently selected table. Activate the
Command window and type the following command.
BROWSE FIELDS CODE, COMPANY, STATUS.STATUS, STATUS.LIMIT
Press Enter to execute the command.
As you see the Browse window now treats both tables as if they are one
table. For each record in the controlling (parent ) file, additional
entries are shown until all the child records are displayed on-screen.
( To distinguish between the entries that belong to different parent
records, these additional entries use record blocks shaded with
diamond symbols instead of repeating the information in the parent
table's fields.)

Fig 7.13 Result of 1-to-many relationship


CREATING ONE-TO-MANY RELATIONS WITH RQBE
You created one to many views of your data by using the SET
RELATION and SET SKIP commands. It is even easier to create one to
many relations with the RQBE. You cannot, however achieve the
intuitive look of the integrated one to many browse windows that
FoxPro creates from direct commands-with shaded blocks indicating
the extent of each parents child records, with RQBE however you can
obtain the same information in fewer steps by enabling FoxPro and
SQL to do some of the work for you. Also with RQBE you need not have
any indexes on the tables as you did with the SET RELATION method.
To see a short example of one to many views you have to create a new
query. If you have not already done so, close any open tables. Next,

118

Chapter 7: Relational Power of FoxPro

create a new query. When FoxPro prompts you for a file, select the
COMPANY.DBF table.
You now have a query with the COMPANY table open. Now try to add
STATUS.DBF in tables. Because you want to relate the COMPANY
table to the STATUS table, you have to add the STATUS table and join
them on their common CODE number (CODE) field. Once again, the
RQBE recognizes this relationship so you simply have to select OK in
the Join Condition dialog box.

Fig 7.14 Using RQBE join condition


Next to obtain useful output, open the Select Fields dialog box by
clicking on the Field check box and select the following fields :
COMPANY.CODE
COMAPNY.COMPANY
STATUS.STATUS
STATUS.LIMIT
If you were to click Do Query now, you would receive a random list of
companies and their categories. Ordering by COMPANY, however,
places all the codes for each company next to each other. To do this,
open the Order By dialog box move COMPANY.CODE to the Ordering
Criteria list.

Fig 7.15 Setting Order by option

Chapter 7 : Relational Power of FoxPro

119

Now Return to the RQBE screen. Click Do Query now, your output
should look like that shown in figure.

Fig 7.16 Result of RQBE

Vous aimerez peut-être aussi