When you include a view or derived table in a key join, SQL Anywhere follows the same basic procedure as with tables, but with these differences:
A derived table works identically to a view. The only difference is that instead of referencing a predefined view, the definition for the table is included in the statement.
For information about recursive table expressions, see Recursive common table expressions, and RecursiveTable algorithm (RT).
For example, in the following statement, View1 is a view.
SELECT * FROM View1 KEY JOIN B; |
The definition of View1 can be any of the following and result in the same join condition to B. (The result set will differ, but the join conditions will be identical.)
SELECT * FROM C CROSS JOIN D; |
or
SELECT * FROM C,D; |
or
SELECT * FROM C JOIN D ON (C.x = D.y); |
In each case, to generate a join condition for the key join of View1 and B, SQL Anywhere considers the table-pairs C-B and D-B, and generates a single join condition. It generates the join condition based on the rules for multiple foreign key relationships described in Key joins of table expressions, except that it looks for a foreign key with the same name as the correlation name of the view (rather than a table referenced in the view).
Using any of the view definitions above, you can interpret the processing of View1 KEY JOIN B
as follows:
SQL Anywhere generates a single join condition by considering the table-pairs C-B and D-B. It generates the join condition according to the rules for determining key joins when there are multiple foreign key relationships:
Assume this generated join condition is B.y = D.z
. You can now expand the original join. For example, the following two statements are equivalent:
SELECT * FROM View1 KEY JOIN B; SELECT * FROM View1 JOIN B ON B.y = View1.z; |
See Key joins when there are multiple foreign key relationships.
The following view contains all the employee information about the manager of each department.
CREATE VIEW V AS SELECT Departments.DepartmentName, Employees.* FROM Employees JOIN Departments ON Employees.EmployeeID = Departments.DepartmentHeadID; |
The following query joins the view to a table expression.
SELECT * FROM V KEY JOIN ( SalesOrders, Departments FK_DepartmentID_DepartmentID ); |
The following query is equivalent to the previous query:
SELECT * FROM V JOIN ( SalesOrders, Departments FK_DepartmentID_DepartmentID ) ON ( V.EmployeeID = SalesOrders.SalesRepresentative AND V.DepartmentID = FK_DepartmentID_DepartmentID.DepartmentID ); |
Send feedback about this page via email or DocCommentXchange | Copyright © 2008, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.0 |