KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Relations are not required in 4D v11 SQL
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: February 18, 2009

In 4D 2004 (and previous versions) implicit relations were used. Here is an example of a query that uses an implicit relation:

QUERY([Emp];[City]Name="San Jose")


Note that there is no information in this query statement to indicate how the two tables are related (i.e. by which primary and foreign key fields are they related?). In fact, without looking at the Structure Editor, it is impossible to determine the relation path being used. What if there is more than one path? Or more than one related field? These questions cannot be answered without looking at the structure editor.

Of course 4D v11 SQL still features support for these implicit relations. However, with support for SQL natively in 4D, it is now possible to use explicit relations. Here is an example of an explicit relation in SQL:

SELECT Name FROM City
WHERE Emp.CityID = City.ID


In this example the relation is explicit; the 'CityID' field in the Emp table is related to the 'ID' field in the City table. Note that a relation line between these two tables is *completely* unnecessary. 4D's SQL engine does not use the relation lines at all because relations are always explicit in SQL queries.

This feature has been extended to the 4D language in version 11 by enhancing the QUERY BY FORMULA command. This command now supports syntax that allows the developer to specify the relation path to follow, regardless of the relation lines. Here is an example:

QUERY BY FORMULA([Emp];[City]Name="San Jose" & [Emp]CompID=[Company]ID & [Company]CityID=[City]ID)


The parameters to QUERY BY FORMULA can be seen as two distinct parts. The first part is the query itself:

QUERY BY FORMULA([Emp];[City]Name="San Jose"


The second is the relation path:

& [Emp]CompID=[Company]ID & [Company]CityID=[City]ID)


This example creates an explicit relation between the [Emp] and [Company] table, and also between the [Company] and [City] table.

Please note: Relation lines are still required for automatic relations as well as any features that rely on automatic relations (Quick Report editor, Auto-wildcard support, etc.).

Commented by Josh Fletcher on February 18, 2009 at 11:14 AM
Be sure to check out http://kb.4d.com/search/assetid=75164 as well.