|
<< Click to Display Table of Contents >> Linking tables |
![]() ![]()
|
Linked cursors allow you to create one-to-many (master-detail) relationships between tables. The cursors on two tables can be linked if the tables share a common field, which must be indexed in the detail table. Linking the cursors on a master table and a detail table forces the cursor on the detail table to make visible only those records containing a key value that matches the key value of the current record in the master table.
For example, a CUSTOMER table (master) and an ORDERS table (detail) share a common field called CUSTOMER_NO. If the current record in the master table has a CUSTOMER_NO of 1221, then the only records visible in the detail table are those that have a CUSTOMER_NO of 1221. In other words, the application sees only the orders that are associated with the current customer.
A master table can be linked to more than one detail table; a detail table can be linked to only one master table. A detail table can also be a master table, linked to other detail tables.
Links apply to all available driver types; they can be established between tables of the same or different driver types.
Setting up the link
To link two tables, the application follows these steps:
1.The application opens cursors on both tables. The detail table cursor must have a current active index on the field that will be used to link the cursors.
2.The application calls DbiBeginLinkMode for each cursor to be linked. The function returns a new cursor.
3.The application calls DbiLinkDetail, passing the cursor handles of both the master and detail tables. The data types of linked fields in master and detail records must match. This function links only on indexes that are applied on fields within the detail table (no expression indexes). For expression links in dBASE and FoxPro tables, call DbiLinkDetailToExp.
4.The two cursors are now linked. When the position of the master cursor changes, the corresponding detail cursor changes to show the applicable records.
Breaking the link
To break the link between the cursors, the application follows these steps:
1.The application calls DbiUnLinkDetail, passing the cursor handle of the detail table. The detail table is now unlinked to any master table, and its cursor displays the entire record range again.
2.The application calls DbiEndLinkMode for each linked cursor, passing it the cursor handle. A standard cursor handle is returned.