Linking Tables in a Set
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.
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.
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.
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.
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.
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.
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:
Open the AlphaMovies database.
In the AlphaMovies Control Panel, click the New button, and select New Table. The Table Editor appears.
Enter the following information in the blank, Table Editor window.
Specify the table name "Invoice" and save it in the AlphaMovies directory. The Invoice table object appears in the AlphaMovies Control Panel.
Enter the following data in the Invoice table.
Specify the table name "Items", and save it in the AlphaMovies directory. The Items table object appears in the AlphaMovies Control Panel.
Enter the following data in the Items table.
Next, you create a new set, using the Invoice table as the primary table.
In the AlphaMovies Control Panel, click the New button, and select New Set.
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
Click Add Link. The Define Link dialog box appears.
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.
Select "Customer_ID", in the Parent Key drop-down list box. "Customer_ID" appears in the Child Key list box.
Select the One-to-One Link Type.
Keep the other default values, and click OK. The Invoice and Customers tables appear in the Set Editor.
Click Add Link.
Select the "Items" table from the Child Table drop-down list box.
Select "Invoice_Number" in the Parent Key drop-down list box. "Invoice_Number" appears in the Child Key list box.
Select the One-to-Many Link Type.
Keep the other default values, and click OK. The Set Editor appears showing the tables in the Invoice set and their link types.
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.
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.
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.
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:
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.
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.
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.
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).
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:
Open the AlphaSports database and Control Panel.
Right-click on the Invoice set, and select Edit Set.
Select the Customer Table.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.
Click the Edit Link button to see Define Link dialog box.
Examine the structure of other AlphaSports sets, and how their links are defined.
Close the Set Editor and the AlphaSports Control Panel.
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.