ASA SQL User's Guide
Joins: Retrieving Data from Several Tables
Key joins
When Adaptive Server Anywhere attempts to generate a join condition based on a foreign key relationship, it sometimes finds more than one relationship. In these cases, Adaptive Server Anywhere determines which foreign key relationship to use by matching the role name of the foreign key to the correlation name of the primary key table that the foreign key references.
The following sections describe how Adaptive Server Anywhere generates join conditions for key joins. This information is summarized in Rules describing the operation of key joins.
A correlation name is the name of a table or view that is used in the FROM clause of the query—either its original name, or an alias that is defined in the FROM clause.
A role name is the name of the foreign key. It must be unique for a given foreign (child) table.
If you do not specify a role name for a foreign key, the name is assigned as follows:
If there is no foreign key with the same name as the primary table name, the primary table name is assigned as the role name.
If the primary table name is already being used by another foreign key, the role name is the primary table name concatenated with a zero-padded three-digit number unique to the foreign table.
If you don't know the role name of a foreign key, you can find it in Sybase Central by expanding the database container in the left pane. Select the table in left pane, and then click the Foreign Keys tab in the right pane. A list of foreign keys for that table appears in the right pane.
See Sample database schema for a diagram that includes the role names of all foreign keys in the sample database.
Adaptive Server Anywhere looks for a foreign key that has the same role name as the correlation name of the primary key table:
If there is exactly one foreign key with the same name as a table in the join, Adaptive Server Anywhere uses it to generate the join condition.
If there is more than one foreign key with the same name as a table, the join is ambiguous and an error is issued.
If there is no foreign key with the same name as the table, Adaptive Server Anywhere looks for any foreign key relationship, even if the names don't match. If there is more than one foreign key relationship, the join is ambiguous and an error is issued.
In the sample database, two foreign key relationships are defined between the tables employee and department: the foreign key ky_dept_id in the employee table references the department table; and the foreign key ky_dept_head in the department table references the employee table.
The following query is ambiguous because there are two foreign key relationships and neither has the same role name as the primary key table name. Therefore, attempting this query results in the syntax error SQLE_AMBIGUOUS_JOIN (-147)
.
SELECT employee.emp_lname, department.dept_name FROM employee KEY JOIN department
This query modifies the query in Example 1 by specifying the correlation name ky_dept_id for the department table. Now, the foreign key ky_dept_id has the same name as the table it references, and so it is used to define the join condition. The result includes all the employee last names and the departments where they work.
SELECT employee.emp_lname, ky_dept_id.dept_name FROM employee KEY JOIN department AS ky_dept_id
The following query is equivalent. It is not necessary to create an alias for the department table in this example. The same join condition that was generated above is specified in the ON phrase in this query:
SELECT employee.emp_lname, department.dept_name FROM employee JOIN department ON department.dept_id = employee.dept_id
If the intent was to list all the employees that are the head of a department, then the foreign key ky_dept_head should be used and Example 1 should be rewritten as follows. This query imposes the use of the foreign key ky_dept_head by specifying the correlation name ky_dept_head for the primary key table employee.
SELECT ky_dept_head.emp_lname, department.dept_name FROM employee AS ky_dept_head KEY JOIN department
The following query is equivalent. The join condition that was generated above is specified in the ON phrase in this query:
SELECT employee.emp_lname, department.dept_name FROM employee JOIN department ON department.dept_head_id = employee.emp_id
A correlation name is not needed if the foreign key role name is identical to the primary key table name. For example, we can define the foreign key department for the employee table:
ALTER TABLE employee ADD FOREIGN KEY department (dept_id) REFERENCES department (dept_id)
Now, this foreign key relationship is the default join condition when a KEY JOIN is specified between the two tables. If the foreign key department is defined, then the following query is equivalent to Example 3.
SELECT employee.emp_lname, department.dept_name FROM employee KEY JOIN department
If you try this example in Interactive SQL, you should reverse the change to the sample database with the following statement:
ALTER TABLE employee DROP FOREIGN KEY department