vfp60l1ch02

  • Upload
    dtrivi

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

  • 8/8/2019 vfp60l1ch02

    1/16

    Visual FoxPro Databases

    Programming Fundamentals of Visual FoxPro 6.0 2-1Copyright© 1999 by Application Developers Training CompanyAll rights reserved. Reproduction is strictly prohibited.

    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.

  • 8/8/2019 vfp60l1ch02

    2/16

    Visual FoxPro Databases

    2-2 Programming Fundamentals of Visual FoxPro 6.0Copyright© 1999 by Application Developers Training Company

    All rights reserved. Reproduction is strictly prohibited.

    Understanding DatabaseTerminology

    Visual FoxPro is a database development product that provides a powerfullanguage and flexible visual tools. The language and tools are designed tomake it easy to produce a database management application.

    When you want to produce an application that manages data, you have tostart 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 thestructures where your data is stored. Table 1 defines a number of terms usedto talk about database design in Visual FoxPro. It is important to understandthe meaning of these terms as they apply to Visual FoxPro.

  • 8/8/2019 vfp60l1ch02

    3/16

    Understanding Database Terminology

    Programming Fundamentals of Visual FoxPro 6.0 2-3Copyright© 1999 by Application Developers Training CompanyAll rights reserved. Reproduction is strictly prohibited.

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

  • 8/8/2019 vfp60l1ch02

    4/16

    Visual FoxPro Databases

    2-4 Programming Fundamentals of Visual FoxPro 6.0Copyright© 1999 by Application Developers Training Company

    All rights reserved. Reproduction is strictly prohibited.

    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 needto do some analysis and planning in order to have some idea of what you areabout to create.

    Planning Your Database

    Usually, you will be working on the production of an application of somekind when you start the database design process. An application can containone or more databases, so the first thing you need to determine is how manydatabases 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 anaccounts payable, accounts receivable, and a general ledger module. Thesemodules can easily map to three databases, one for each module.

    Considering the accounts receivable database, you might find that thisdatabase needs tables for storing customer information, invoices, and cashreceipts. In considering the customer table, you might find you need fieldsfor 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 referredto as the top down approach. Using this approach, you start the design

     process at the highest level, in this example the application, and then stepdown one level to the databases and designs that you need. Once thedatabases have been identified, the next step is to determine the tables that a

     particular database requires. When the tables are known, you design thefields needed for each table.

    After following this top down approach for each database, you will havedesigned the complete system data requirements.

    Understanding Keys

    One of the areas that can make or break a database application design effortis the area of keys. It is easy to get caught up in the details of therequirements, forgetting that you need a primary key for every table youcreate.

  • 8/8/2019 vfp60l1ch02

    5/16

    Steps to Database Design

    Programming Fundamentals of Visual FoxPro 6.0 2-5Copyright© 1999 by Application Developers Training CompanyAll rights reserved. Reproduction is strictly prohibited.

    When you determine the field required for a table, you must ensure that thereis some field or combination of fields that provides a unique value for everyrecord in that table. Your program will use this field or combination of fieldsto find any given record in the table. It will also be used to createrelationships with records in other tables.

    Sometimes, it may be necessary to place a field in the table design to act asthe primary key even though it has no other purpose in the table.

  • 8/8/2019 vfp60l1ch02

    6/16

    Visual FoxPro Databases

    2-6 Programming Fundamentals of Visual FoxPro 6.0Copyright© 1999 by Application Developers Training Company

    All rights reserved. Reproduction is strictly prohibited.

    The Database Wizard

    The Database Wizard steps you through the process of creating a newdatabase. The Database Wizard is most useful in situations where the data

    that you are interested in storing closely matches one of the wizard’s predefined databases. The wizard is also useful in understanding whatdatabases are and how they are defined in Visual FoxPro.

    Step 1 – Select a Database

    Visual FoxPro contains a variety of predefined databases ranging fromaccounting needs to household management to retail inventory needs. The

    Step 1 − Select a Database page allows you to select one of these predefineddatabases. Choose the one that most closely resembles the data you are

    interested in storing.

    In this example, you’ll 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 tocontinue.

  • 8/8/2019 vfp60l1ch02

    7/16

    The Database Wizard 

    Programming Fundamentals of Visual FoxPro 6.0 2-7Copyright© 1999 by Application Developers Training CompanyAll rights reserved. Reproduction is strictly prohibited.

    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 you’ve become accustomed to

    using the Database Designer you’ll 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. Bydefault all the tables are selected. Clear the check boxes of any tables thatyou don’t want the Wizard to create. Figure 2 shows the selected tables for the Music Collection database on the Select Tables and Views page of theDatabase 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 theTracks table. At this time, you will use the indexes that the wizard hasselected. In the “Working with Tables” section of the chapter, “Creating

  • 8/8/2019 vfp60l1ch02

    8/16

    Visual FoxPro Databases

    2-8 Programming Fundamentals of Visual FoxPro 6.0Copyright© 1999 by Application Developers Training Company

    All rights reserved. Reproduction is strictly prohibited.

    Visual FoxPro Tables”, you will learn about indexes and why you may wantto 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 RelationshipsStep 4 sets up the relationships between the tables in the database.Relationships show how data is related between the different tables. You canalter the relationships by clicking the Relationships  button. Figure 4 showsthe Set up relationships page for the Music Collections table of the DatabaseWizard. 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.

  • 8/8/2019 vfp60l1ch02

    9/16

    The Database Wizard 

    Programming Fundamentals of Visual FoxPro 6.0 2-9Copyright© 1999 by Application Developers Training CompanyAll rights reserved. Reproduction is strictly prohibited.

    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.

  • 8/8/2019 vfp60l1ch02

    10/16

    Visual FoxPro Databases

    2-10 Programming Fundamentals of Visual FoxPro 6.0Copyright© 1999 by Application Developers Training Company

    All rights reserved. Reproduction is strictly prohibited.

    The Database Wizard Relationships dialog box has three options. The defaultoption is that the tables are not related. The second option defines a one-to-many 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 notincluding 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 thedatabase and open it again in the Database Designer for further customization. Additionally, you can have the wizard populate the tableswith a few sample records by selecting the Populate tables with sample datacheck box.

    We opted to open the database in the Database Designer. In the next section,you’ll learn how to further modify the data.

  • 8/8/2019 vfp60l1ch02

    11/16

    The Database Wizard 

    Programming Fundamentals of Visual FoxPro 6.0 2-11Copyright© 1999 by Application Developers Training CompanyAll rights reserved. Reproduction is strictly prohibited.

    Figure 6. Saving and populating the database with data.

  • 8/8/2019 vfp60l1ch02

    12/16

    Visual FoxPro Databases

    2-12 Programming Fundamentals of Visual FoxPro 6.0Copyright© 1999 by Application Developers Training Company

    All rights reserved. Reproduction is strictly prohibited.

    The Visual FoxPro DatabaseDesigner 

    The Database Designer, shown in Figure 7, is the primary tool for managingDatabase 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 theFile menu and choose NewDatabase , and click the New button. Or, youcan use commands in the Command window. The command Create

    Database   followed by the command Modify

    Database opens the Database Designer.

  • 8/8/2019 vfp60l1ch02

    13/16

    The Visual FoxPro Database Designer 

    Programming Fundamentals of Visual FoxPro 6.0 2-13Copyright© 1999 by Application Developers Training CompanyAll rights reserved. Reproduction is strictly prohibited.

    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 areavailable on the Database menu that is added to the Visual FoxPro Systemmenu whenever the Database Designer is open. (3) Right-clicking the

     background of the Database Designer displays a shortcut menu with all theoptions 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 StoredProcedures

    Edits the database’s stored procedures.

    Table 2. The options for the Database Designer.

  • 8/8/2019 vfp60l1ch02

    14/16

    Visual FoxPro Databases

    2-14 Programming Fundamentals of Visual FoxPro 6.0Copyright© 1999 by Application Developers Training Company

    All rights reserved. Reproduction is strictly prohibited.

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

  • 8/8/2019 vfp60l1ch02

    15/16

    The Visual FoxPro Database Designer 

    Programming Fundamentals of Visual FoxPro 6.0 2-15Copyright© 1999 by Application Developers Training CompanyAll rights reserved. Reproduction is strictly prohibited.

    Summary

    •  Understanding database terminology is an important first step whenlearning about database design.

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

    •  The top down approach to designing databases helps you to defineall levels of an application starting with the highest and movingdown to the lowest.

    •  A primary key is needed for every table you create.

    •  The Database Wizard contains many predefined databases to helpyou get started creating databases.

    •  The Database Designer helps you to further customize the database

    you created using the Wizard.

  • 8/8/2019 vfp60l1ch02

    16/16

    Visual FoxPro Databases

    2-16 Programming Fundamentals of Visual FoxPro 6.0Copyright© 1999 by Application Developers Training Company

    All rights reserved. Reproduction is strictly prohibited.