| Stonefield Query SDK |
| Creating a Subtable |
select ITEMS.NAME, GLACCOUNT.NAME, EXPENSE.NAME
from ITEMS
inner join GLACCOUNT
on ITEMS.REVACCOUNT = GLACCOUNT.ACCOUNTNUM
inner join GLACCOUNT EXPENSE
on ITEMS.EXPACCOUNT = EXPENSE.ACCOUNTNUMSpecifying EXPENSE as the alias for the GLACCOUNT table when it's used for expense accounts allows the database engine to handle the two uses for this table.
Stonefield Query provides the ability to define aliases for tables by defining a "subtable" of a table. A subtable is really just a way of assigning an alias to a table so it can be used more than once in a SQL statement, but provides some additional capabilities as well:
The Customers table has a foreign key called CustomerType that contains the ID value for a record in Lookups representing the customer type, and another foreign key called LeadSource that contains the ID value for a record in Lookups representing how they became a customer. Thus, Lookups contains two different types of records: customer types and lead sources. The RecType column in Lookups contains a "C" for customer type records and "L" for lead source records.
To handle this data design in Stonefield Query, create a subtable of Lookups called LeadSources and define a relationship between Customers and LeadSources. However, what if the user selects only fields from the LeadSources table ("Let's create a report listing all lead source descriptions")? That report prints all records in Lookups, not just lead source records. To solve that problem, Stonefield Query allows you to define a filter for the subtable. This filter expression is automatically added to the WHERE clause of the SQL statement so only certain records are retrieved from the database. In this case, the LeadSources subtable has LeadSources.RecType = "L" as its Filter property. So, when the user creates a report from the LeadSources table, only lead source records are displayed. You might also want to create another subtable of Lookups called CustomerTypes, set its Filter to CustomerTypes.RecType = "C," and then specify that Lookups is not reportable. Thus, the user only sees Customers, CustomerTypes, and LeadSources tables, and not a Lookups table.
Subtables aren't just useful for multiple relationships between a pair of tables; they can also be used for self-joins. For example, you may have an Employees table that includes a ManagerID field which contains the ID for another employee record representing the employee's manager. In this case, create a subtable of Employees called Managers and define a relationship between them matching the ManagerID field from Employees with the ID field from Managers. You may also want only certain fields in Managers to be available, such as their name, since salary, department, and other fields can be obtained from the appropriate Employees record. You may also set a Filter for Managers, something like Managers.IsManager = .T.
To create a subtable for a table, select the table in the TreeView and choose the Create Subtable function from the Objects menu, the shortcut menu, or from the drop-down list for the Create button. Stonefield Query Studio creates a copy of the table and all of its fields, with a default name of "Subtable" (where table is the name of the original table). You are prompted if you want to copy the relations for the existing table to the subtable. Subtables have a different icon in the TreeView (
) and a different set of properties.
In addition to the usual properties for tables, subtables also have the following properties:
Stonefield Query may automatically create subtables when it creates the meta data for a database; see the Adding a Database to the Data Dictionary topic for details.
See Also
Adding a Database to the Data Dictionary | Table Properties
| Last Updated: 03/23/10 |