Linking Tables in a Set

Description

A set is a group of tables, linked together so that you can search, report, and view data from component tables, as if they were a single table. When entering data in a set, Alpha Anywhere places the information in the correct table, along with the necessary linking information. You can create layouts, such as forms, browses, and reports, and perform and save operations for a set. In this lesson, you learn about sets, and create a set for AlphaMovies data.

You should have already created the AlphaMovies database in the Creating a Database lesson.

Understanding Sets 

A set is a group of tables, linked to each other, by a common field or expression. For example, you might use a set to create an invoicing system, such that you have three tables: one that stores invoice information, one that stores customer information, and one that stores information about each item purchased.

Invoice Table 

images/atable8.png

Customer Table 

images/atable9.png

The Invoice and Customer tables are related through the Customer_No field, which is referred to as the linking field. The Items table contains information about the items purchased. The Items table is linked to the Invoice table through the Invoice_No field.

Items Table 

images/atable10.png

Connecting the Tables 

When you create a set, you link pairs of tables together using either one-to-one or one-to-many links. You build a set by starting with a single table, called the primary table. You link the other tables, called child tables, to the primary table. Tables linked from the child tables are then called grandchild tables, and so on down the hierarchy.

  • One-to-One Links 

    In a one-to-one link, there is only one matching child record for each parent record. For example, in the invoice set described previously, the Invoice and Customer tables have a one-to-one relationship. For each invoice, there can only be one customer. The Customer_No field, which is found in both tables, is the linking field for the Invoice and Customer tables.

  • One-to-Many Links 

    In a one-to-many link, there can be multiple matching records for each parent record. In the case of the invoice set, the Invoice and Items tables have a one-to-many relationship. For each invoice, there can be multiple items purchased. The link between the Invoice table and the Items table is the Invoice_No field, which is found in both tables.

Creating Sets 

When designing tables for a set, you must keep set links in mind. Each pair of linked tables must have a linking field or expression that evaluates to the same value. Linking fields and expressions specify information that the parent and child tables have in common. The fields and expressions must evaluate to the same field type, such as Character, Numeric, or such; they do not have to have the same field name. To create a set, you use the Set Editor, a visual tool that you use to designate the tables, and define the table links in a set. The Set Editor displays one-to-one links as single lines, one-to-many links as double lines, and one-to-many links for which referential integrity has been assigned, as thick lines. In the next procedure, you create an Invoice table and an Items table for AlphaMovies. Then you create a new set, using the Invoice table as the primary table. To create a new set:

  1. Open the AlphaMovies database.

  2. In the AlphaMovies Control Panel, click the New button, and select New Table. The Table Editor appears.

  3. Enter the following information in the blank, Table Editor window.

    images/atable11.png
  4. Specify the table name "Invoice" and save it in the AlphaMovies directory. The Invoice table object appears in the AlphaMovies Control Panel.

  5. Enter the following data in the Invoice table.

    images/atable12.png
  6. Specify the table name "Items", and save it in the AlphaMovies directory. The Items table object appears in the AlphaMovies Control Panel.

  7. Enter the following data in the Items table.

    images/atable13.png
  8. Next, you create a new set, using the Invoice table as the primary table.

  9. In the AlphaMovies Control Panel, click the New button, and select New Set.

  10. Select the Invoice table in the Select the Primary Table for the Set dialog box, and click OK. The Set Editor appears, containing the Invoice table. Picture

    images/setEditor.gif
  11. Click Add Link. The Define Link dialog box appears.

    images/linkDefine.gif
  12. You use this dialog box to define the link between the AlphaMovies Invoice table and the Customers table. Select the "Customers" table from the Child Table drop-down list box.

  13. Select "Customer_ID", in the Parent Key drop-down list box. "Customer_ID" appears in the Child Key list box.

  14. Select the One-to-One Link Type.

    images/Set_Editor_Define_Link.gif
  15. Keep the other default values, and click OK. The Invoice and Customers tables appear in the Set Editor.

    images/setInvoice.gif
  16. Click Add Link.

  17. Select the "Items" table from the Child Table drop-down list box.

  18. Select "Invoice_Number" in the Parent Key drop-down list box. "Invoice_Number" appears in the Child Key list box.

  19. Select the One-to-Many Link Type.

    images/linkDefine4.gif
  20. Keep the other default values, and click OK. The Set Editor appears showing the tables in the Invoice set and their link types.

    images/setInvoice4.gif
  21. Close the Set Editor. The Control Panel shows the two tables you created, Invoices and Items, along with the Invoice set that you created. The picture below shows icons. Click the View button to change the Control Panel to display text.

    images/setInvoice3.gif

Using the Set Editor 

You use the Set Editor to specify and edit table linking parameters. In the Control Panel, right-click on the Invoice set, and select Edit Set to open the Set Editor.

  • About Linking Fields 

    Linking fields do not have to be a single field. Suppose the Invoice table had two fields.

    • The first field, Tran_Type, would indicate whether the transaction is a sale or refund. It would contains the value: "S" or "R".

    • The second field, Slip_Num, would store an incrementing number, such as: 001, 002, 003.

    • In this case the invoice number would consist of the first field plus the second field, resulting in invoice numbers, such as: "S001", "R002", and "S003". However, the Items table has just one field to store the invoice number. In the Set Editor, to link the Invoice and Items tables, you would specify Tran_Type+Slip_Num as the Parent Linking Field, and Inv_No as the Child Linking Field.

  • Linking Type 

    In the Link Type drop-down list box you specify whether the link is one-to-one or one-to-many. There are three options for one-to-one links:

    • first match

    • closest match

    • last match

    • You use the options for situations in which there are multiple matching records or no matching records. If you select first match or closest match, and there are multiple matching records, Alpha Anywhere links to the first matching record in the child table. If you select last match, Alpha Anywhere links to the last matching record. When there are no matching records, and you select closest match, Alpha Anywhere links to the record with the closest matching key value.

  • Linking Options 

    Using linking options, you can filter out child records based on a condition you specify, exclude child records with duplicate linking key values, and reverse the order of one-to-many records, so that records entered most recently appear before records entered earlier.

  • Filter expressions 

    By specifying a filter expression, you can conditionally exclude records from the child table. For example, you create a set with Customer as the primary table and Transaction as the child table. You can create a filter that selects only Transaction records that were entered within the last year.

  • Ordering options 

    • Unique - By selecting the Unique check box, records with duplicate linking keys are filtered out. This can speed up set performance for tables linked using a one-to-one link, since Alpha Anywhere does not have to search through as many records. The Unique option is not appropriate for tables linked using a one-to-many link, since one-to-many is used specifically to link multiple records with the same linking key value.

    • Descending order - You use Descending order with tables linked using a one-to-many link, in which you want the child records listed in order from older to more recently entered, also known as First In Last Out (FILO).

Looking at AlphaSports Sets 

There are five sets defined in the AlphaSports application. In the next procedure, you examine the sets and their links, using the Set Editor. To view AlphaSports sets:

  1. Open the AlphaSports database and Control Panel.

    images/AS_ControlPanel.gif
  2. Right-click on the Invoice set, and select Edit Set.

  3. Select the Customer Table.

    images/Set_Editor_Table_Links_chart.gif
    In the information fields at the bottom of the window, the Set Editor displays the directory path to the table DBF file, the Link Type, and the Parent and Child Linking Expressions.
  4. Click the Edit Link button to see Define Link dialog box.

  5. Examine the structure of other AlphaSports sets, and how their links are defined.

  6. Close the Set Editor and the AlphaSports Control Panel.

In later lessons, you enter invoice data in a custom form designed for the AlphaSports Invoice set.

Summary 

In this lesson you learned about sets, and created a set for AlphaMovies. You learned about table linking, and one-to-one and one-to-many links. You used the Set Editor to create an Invoice set for AlphaMovies, and examined the sets in AlphaSports. See Customizing Browses to learn how to use and create Custom Browses for tables and sets.

See Also