You are on page 1of 16

Visual FoxPro Databases

Visual FoxPro
Databases
Objectives

Understand database terminology.

Learn how to create a database.

Learn how to use the Database wizard.

Learn about managing database containers.

Programming Fundamentals of Visual FoxPro 6.0


Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

2-1

Visual FoxPro Databases

Understanding Database
Terminology
Visual FoxPro is a database development product that provides a powerful
language and flexible visual tools. The language and tools are designed to
make it easy to produce a database management application.
When you want to produce an application that manages data, you have to
start by creating structures to store your data. The structure that stores your
data affects how easily you can manipulate and present that data to the user.
This section discusses the tools in Visual FoxPro that you use to create the
structures where your data is stored. Table 1 defines a number of terms used
to talk about database design in Visual FoxPro. It is important to understand
the meaning of these terms as they apply to Visual FoxPro.

2-2

Programming Fundamentals of Visual FoxPro 6.0


Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

Understanding Database Terminology

Term

Definition

Database

A collection of tables and other data formats that comprise a set of


related data. For example, you might have an Accounts Receivable
database that includes Customer, Invoice, Inventory, and Cash
Receipts tables.

Table

A data structure that is organized in columns and rows, and stores


information about a single type of person, place, thing, or concept.
Examples of tables are a Customer table, an Invoice table, or an
Inventory table.

Record

A single row of a table including all the columns in that row. A


record describes a single instance of the person, place, thing, or
concept that the table relates to. In a Customer table, a record
includes all the information in the table about a single customer,
such as name, address, phone number, and date of their last
purchase.

Field

A single column of a table. Fields store similar information for


different instances of the person, place, thing, or concept to which
the table is related. Examples of fields are name, invoice number, or
inventory quantity.

Key

A key is a field or group of fields used to identify a single record


within a table. There are different kinds of keys discussed in the
section, Steps to Database Design. Here the key refers to the
primary key.

Index

An index is an external file that provides the ability to see the


records in a table in different sort orders without altering the
physical order of those records. Indexes will be defined in much
more detail in the Working With Tables section of the chapter,
Creating Visual FoxPro Tables.

Table 1. Database design terminology.

Programming Fundamentals of Visual FoxPro 6.0


Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

2-3

Visual FoxPro Databases

Steps to Database Design


Before you start your car to drive somewhere, you usually have some idea of
where you intend to go. Designing a database is no different. Before you sit
down at the computer to create the files you will use to store data, you need
to do some analysis and planning in order to have some idea of what you are
about to create.

Planning Your Database


Usually, you will be working on the production of an application of some
kind when you start the database design process. An application can contain
one or more databases, so the first thing you need to determine is how many
databases will be included in your application.
The databases should contain tables that are somehow related to each other.
For example in an accounting application you might find that you need an
accounts payable, accounts receivable, and a general ledger module. These
modules can easily map to three databases, one for each module.
Considering the accounts receivable database, you might find that this
database needs tables for storing customer information, invoices, and cash
receipts. In considering the customer table, you might find you need fields
for name, address, city, state, zip code, and phone number.
The pattern that is evolving is one of containership: the table contains fields,
the database contains tables, and the application contains databases. One of
the tried and true approaches to designing computer applications is referred
to as the top down approach. Using this approach, you start the design
process at the highest level, in this example the application, and then step
down one level to the databases and designs that you need. Once the
databases have been identified, the next step is to determine the tables that a
particular database requires. When the tables are known, you design the
fields needed for each table.
After following this top down approach for each database, you will have
designed the complete system data requirements.

Understanding Keys
One of the areas that can make or break a database application design effort
is the area of keys. It is easy to get caught up in the details of the
requirements, forgetting that you need a primary key for every table you
create.
2-4

Programming Fundamentals of Visual FoxPro 6.0


Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

Steps to Database Design


When you determine the field required for a table, you must ensure that there
is some field or combination of fields that provides a unique value for every
record in that table. Your program will use this field or combination of fields
to find any given record in the table. It will also be used to create
relationships with records in other tables.
Sometimes, it may be necessary to place a field in the table design to act as
the primary key even though it has no other purpose in the table.

Programming Fundamentals of Visual FoxPro 6.0


Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

2-5

Visual FoxPro Databases

The Database Wizard


The Database Wizard steps you through the process of creating a new
database. The Database Wizard is most useful in situations where the data
that you are interested in storing closely matches one of the wizards
predefined databases. The wizard is also useful in understanding what
databases are and how they are defined in Visual FoxPro.

Step 1 Select a Database


Visual FoxPro contains a variety of predefined databases ranging from
accounting needs to household management to retail inventory needs. The
Step 1 Select a Database page allows you to select one of these predefined
databases. Choose the one that most closely resembles the data you are
interested in storing.
In this example, youll use the Music Collection database selected on the first
page of the Database Wizard (see Figure 1).

Figure 1. Select the Music Collection database in the Database Wizard.

When you have selected the database that best fits your needs, click Next to
continue.
2-6

Programming Fundamentals of Visual FoxPro 6.0


Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

The Database Wizard


NOTE

If none of the predefined databases is appropriate, then it is


better to use the Database Designer to create your database rather
than the Database Wizard. Once youve become accustomed to
using the Database Designer youll use the Database Wizard less
and less.

Step 2 Select Tables and Views


In Step 2, you select the tables and views to include in the database. By
default all the tables are selected. Clear the check boxes of any tables that
you dont want the Wizard to create. Figure 2 shows the selected tables for
the Music Collection database on the Select Tables and Views page of the
Database Wizard. Confirm your selections, then click Next to continue.

Figure 2. Selecting the Music Collection tables.

Step 3 Index the Tables


Figure 3 shows the Index the Tables page where you set the indexes for the
Tracks table. At this time, you will use the indexes that the wizard has
selected. In the Working with Tables section of the chapter, Creating
Programming Fundamentals of Visual FoxPro 6.0
Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

2-7

Visual FoxPro Databases


Visual FoxPro Tables, you will learn about indexes and why you may want
to add indexes. Click Next to move to Step 4.

Figure 3. Creating the indexes for the Tracks table in the Database Wizard.

Step 4 Set Up Relationships


Step 4 sets up the relationships between the tables in the database.
Relationships show how data is related between the different tables. You can
alter the relationships by clicking the Relationships button. Figure 4 shows
the Set up relationships page for the Music Collections table of the Database
Wizard. Click Next to move to the last page of the Database Wizard.

Tip:

Setting up relationships in new tables is more easily accomplished using


either the Table Wizard or manually in the Database Designer.

2-8

Programming Fundamentals of Visual FoxPro 6.0


Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

The Database Wizard

Figure 4. The relationships for the Music Categories table.

Clicking on the Relationships button launches the Relationships dialog box,


shown in Figure 5.

Figure 5. The Database Wizard Relationships dialog box.

Programming Fundamentals of Visual FoxPro 6.0


Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

2-9

Visual FoxPro Databases


The Database Wizard Relationships dialog box has three options. The default
option is that the tables are not related. The second option defines a one-tomany relationship and the third option defines a many-to-one relationship.
You only need to work with the Relationships dialog box when the table
being related to is a new table that is unknown to the wizard. In this example,
you are using a predefined database that was selected in Step 1, and are not
including any unknown tables.

WARNING!

If the one-to-many or many-to-one options are selected, the wizard


will add additional fields to either the parent or child table to
support the relationship.

Step 5 Finish
Figure 6 shows the final step of the Database Wizard.
Step 5 Finish allows you to save the database. You can choose to save the
database and open it again in the Database Designer for further
customization. Additionally, you can have the wizard populate the tables
with a few sample records by selecting the Populate tables with sample data
check box.
We opted to open the database in the Database Designer. In the next section,
youll learn how to further modify the data.

2-10

Programming Fundamentals of Visual FoxPro 6.0


Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

The Database Wizard

Figure 6. Saving and populating the database with data.

Programming Fundamentals of Visual FoxPro 6.0


Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

2-11

Visual FoxPro Databases

The Visual FoxPro Database


Designer
The Database Designer, shown in Figure 7, is the primary tool for managing
Database Containers. A Visual FoxPro database is a container for
information about the data. The database does not contain the data, but rather
contains information about the data. This information can be in the form of
table structures, relationships, or rules governing the editing of the data.

Figure 7. The Music Collection database, as opened in the Database Designer.

There are two other ways to open the Database Designer. You can select the
File menu and choose NewDatabase, and click the New button. Or, you
can use commands in the Command window. The command Create
Database <DatabaseName> followed by the command Modify
Database opens the Database Designer.

2-12

Programming Fundamentals of Visual FoxPro 6.0


Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

The Visual FoxPro Database Designer

The Database Designer Toolbar


Figure 8 shows a close-up of the Database Designer toolbar.

Figure 8. The Database Designer toolbar.

There are three ways to access the functionality of the Database Designer. (1)
Clicking one of the buttons on the Database Designer toolbar (see Figure 8)
evokes the function described in the callouts. (2) These same operations are
available on the Database menu that is added to the Visual FoxPro System
menu whenever the Database Designer is open. (3) Right-clicking the
background of the Database Designer displays a shortcut menu with all the
options listed in Table 2.

Option

Action

Add Table

Adds a table to the database that already exists on disk.

New Table

Creates a new table and adds it to the database.

New Remote View

Creates a new view that gets its data from a remote source.

Modify Table

Modifies the selected table or view.

Connections

Creates or modifies a connection to a remote data source.

New Local View

Creates a new view that gets its data from local tables.

Browse Table

Opens the selected table in a Browse window.

Remove Table

Removes a table or view from the database and optionally


deletes it from the disk.

Edit Stored
Procedures

Edits the databases stored procedures.

Table 2. The options for the Database Designer.

Programming Fundamentals of Visual FoxPro 6.0


Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

2-13

Visual FoxPro Databases


These options will be covered in more detail as you use them in the
following chapters on designing databases.

2-14

Programming Fundamentals of Visual FoxPro 6.0


Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

The Visual FoxPro Database Designer

Summary

Understanding database terminology is an important first step when


learning about database design.

Before you create a database, analyze and plan the type of database
you want to create.

The top down approach to designing databases helps you to define


all levels of an application starting with the highest and moving
down to the lowest.

A primary key is needed for every table you create.

The Database Wizard contains many predefined databases to help


you get started creating databases.

The Database Designer helps you to further customize the database


you created using the Wizard.

Programming Fundamentals of Visual FoxPro 6.0


Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

2-15

Visual FoxPro Databases

2-16

Programming Fundamentals of Visual FoxPro 6.0


Copyright 1999 by Application Developers Training Company
All rights reserved. Reproduction is strictly prohibited.

You might also like