When both of the two table expressions being joined do not contain commas, SQL Anywhere examines the foreign key relationships in the pairs of tables in the statement, and generates a single join condition.
For example, the following join has two table-pairs, A-C and B-C.
(A NATURAL JOIN B) KEY JOIN C |
SQL Anywhere generates a single join condition for joining C with (A NATURAL JOIN B)
by looking at the foreign key relationships within the table-pairs A-C and B-C. It generates one join condition for the two
pairs according to the rules for determining key joins when there are multiple foreign key relationships:
First, it looks at both A-C and B-C for a single foreign key that has the same role name as the correlation name of one of the primary key tables it references. If there is exactly one foreign key meeting this criterion, it uses it. If there is more than one foreign key with the same role name as the correlation name of a table, the join is considered to be ambiguous and an error is issued.
If there is no foreign key with the same name as the correlation name of a table, SQL Anywhere looks for any foreign key relationship between the tables. If there is one, it uses it. If there is more than one, the join is considered to be ambiguous and an error is issued.
If there is no foreign key relationship, an error is issued.
For more information, see Key joins when there are multiple foreign key relationships.
The following query finds all the employees who are sales representatives, and their departments.
SELECT Employees.Surname, FK_DepartmentID_DepartmentID.DepartmentName FROM ( Employees KEY JOIN Departments AS FK_DepartmentID_DepartmentID ) KEY JOIN SalesOrders; |
You can interpret this query as follows.
SQL Anywhere considers the table expression ( Employees KEY JOIN Departments as FK_DepartmentID_DepartmentID )
and generates the join condition Employees.DepartmentID = FK_DepartmentID_DepartmentID.DepartmentID
based on the foreign key FK_DepartmentID_DepartmentID.
SQL Anywhere then considers the table-pairs Employees/SalesOrders and Departments/SalesOrders. Note that only one foreign
key can exist between the tables SalesOrders and Employees and between SalesOrders and Departments, or the join is ambiguous.
As it happens, there is exactly one foreign key relationship between the tables SalesOrders and Employees (FK_SalesRepresentative_EmployeeID),
and no foreign key relationship between SalesOrders and Departments. So, the generated join condition is SalesOrders.EmployeeID = Employees.SalesRepresentative
.
The following query is therefore equivalent to the previous query:
SELECT Employees.Surname, Departments.DepartmentName FROM ( Employees JOIN Departments ON ( Employees.DepartmentID = Departments.DepartmentID ) ) JOIN SalesOrders ON ( Employees.EmployeeID = SalesOrders.SalesRepresentative ); |
Discuss this page in DocCommentXchange. Send feedback about this page using email. |
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |