Determining relationships among tables
To better define your tables, you need to determine how they relate to each other.
Single versus multiple tables
Each table should have a specific purpose. It’s often better to create several small tables and link them together, rather than try to store everything in one large table. Keeping everything in a single large table usually forces you to store redundant data.
For example, if you stored the complete customer information with each order, you would be entering the customer’s name and address with every order. Not only does this procedure invite errors, but it makes it difficult to update information if something as simple as a customer’s phone number should change. In addition, redundant data wastes disk space.
Use multiple tables to minimize the amount of data that appears more than once. By storing the name and address information once in a Customer table, you have only one location to update if that information changes. When a customer places an order, the order information goes in a separate table that can be linked to the name and address table.
In our sample case, three distinct tables have emerged to contain data: one for customers, another for orders, and one for the items ordered. We can call the tables Customer, Orders, and Lineitem.
One-to-one and one-to-many relationships
With multiple tables in an application, it’s important to understand the relationships among entities and activities. In each relationship, is there a one-to-one correspondence, or does an entry in one correspond to many entries in another? Or is there no direct relationship?
For example, a customer can place several orders, and an order can contain one or more items. These are one-to-many relationships. Each order is associated with one customer, a one-to-one relationship.
For example, a query might relate three tables:
From the Customer table comes the customer name, NAME
From the Orders table comes the order number ORDER_NO
From the Lineitem table comes the stock number STOCK_NO and selling price, SELL_PRICE.
The query results show each customer name followed by many orders, and under the orders, a list of the items and prices in the order.
Parent and child tables
When you relate two tables in a query, form, report, or data module, you establish one as the parent and the other as the child table. As you select a row from the parent table, you see the corresponding child row or rows.
Linking tables in a parent-child relationship lets you easily find rows in the child table. For example, you can set up the Customer table as the parent, and Orders as the child. Then, when you move to a new row in the Customer table, the row pointer in the Orders table moves to the orders for that customer automatically. Similarly, the Orders table becomes the parent to the Lineitem table, so that selecting an order also selects the items in the order.
The parent and child tables are linked on a common field, called the linking field. In our example, a query links the Customer and Orders tables on the CUSTOMER_N (customer number) field. In dBASE Plus, the linking field in the child table must have an index. As the example shows, a single table can be both parent and child in the same query.