TurboDB Engine Documentation
Working with Link and Relation Fields
|Previous Top Next|
In order to profit from automatic linking you should think of adding link and relations columns to every table you create. You will soon find it very natural to add the linking information into the table. After all you do the same with your Delphi, C++ and/or Java classes, don't you?
Adding Link and Relation Columns
When you want to work with link and relations fields to establish a one-to-many or many-to-many relationship between tables, you must decide which of the tables is the source and which is the target of the relationship. The first is called the child table and the second the parent table. It is just like the referencing table and the referenced table when you are working with traditional foreign keys.
The parent table must include an AutoInc column, which is used at the primary key for the linking. The child table must include a link or relation column to establish the relationship. The link column can store exactly one pointer to the parent table. The pointer is displayed as the AutoInc value in the parent table or as the column values of the indication, if you have defined one with the AutoInc column of the parent table. The relation column stores multiple pointers to the parent table, which are displayed as a list of AutoInc values or column values according to the indication definition for the AutoInc column.
Link and Relation Columns with Direct Table Access
(Direct table access is a feature available for the VCL component library but not with ADO.NET.)
Once you have defined your links and relations, they are respected by the database in every query. Even if you don't have any search-criteria, only corresponding detail records will be shown for every master record. In the rare case that you don't want this default linking you may always enter another equate join that overrides it.
Link and Relation Columns with TurboSQL
In TurboSQL queries the relationships defined through link and relation fields are not created automatically. Use a simple JOIN to utilize the reference:
SELECT * FROM Master JOIN Detail ON Detail.LinkField = Master.RecordId
For adding new rows to the tables, the function CurrentRecordId should be used:
INSERT INTO Master VALUES(...); INSERT INTO Detail VALUES(..., CurrentRecordId(Master), ...)
This compound statement inserts first a record in the Master table and then a record into the Detail table while using the last value for the record id in the master table as the new value for the link field in the detail table. Therefore the detail record is linked to the master record.
This feature is only partly supported in TurboDB Managed. TurboDB Managed currently supports link columns but not relation columns.