Set Descartes
Descartes rally produced under the following conditions:
- Omitted connection condition
- Join condition is invalid
- All rows in all tables are interconnected
To avoid a Cartesian set, may be added in effective connection conditions WHERE.
Oracle is connected
using a connection query data in multiple tables.
SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column1 = table2.column2;
- Write connection condition in the WHERE clause.
- When the same column in the table, with the table name prefix before the column name
Equijoins
Example
SELECT employees.employee_id, employees.last_name, employees.department_id, departments.department_id, departments.location_id FROM employees, departments WHERE employees.department_id = departments.department_id;
- A plurality of connection conditions AND operator
Distinguish duplicate column names
- Use table prefix to distinguish the same column in the plurality of tables.
- Columns having the same column name in different tables may be identified by a table alias.
Alias table
- Using aliases, simplify the query.
- Use table prefix can improve the efficiency.
SELECT e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id FROM employees e, departments d WHERE e.department_id = d.department_id;
Connecting a plurality of tables
- Connecting the n-tables, at least n-1 connections conditions. For example: three connection table, at least two connection conditions.
Non-Equijoins
column salary in the EMPLOYEES table should be among the highest wages in JOB_GRADES table with the minimum wage
examples
SELECT e.last_name, e.salary, j.grade_level FROM employees e, job_grades j WHERE e.salary BETWEEN j.lowest_sal AND j.highest_sal;
The inner and outer joins
- En: combined with two or more rows of the same column of a table, the result set does not contain a table and another table rows that do not match
- External connection: two tables satisfy the join condition in addition return line also returns the left (or right) in the table row condition is not satisfied during the connection, this connection is called a left (or right) outer join. No matching row, the corresponding result table is empty (NULL). WHERE clause conditions are similar to the outer connecting the internal connection, but is connected to the back of the list does not match the condition to increase the outer connecting line operator, i.e., with the plus sign in parentheses (+).
- In SQL: 1999, the connector returns only the data that satisfy the join condition
- During the connection the two tables in addition to the return line further satisfy the join condition to return the left (or right) table row condition is not satisfied, this connection is called a left (or right) outer join.
- During the connection the two tables in addition to the return line further satisfy the join condition in the return line condition is not satisfied two tables, this connection is called a full external connection.
Outer join syntax
- Use outer join query data does not meet the join condition.
- It is connected to an outer symbol (+).
右外连接 SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column(+) = table2.column; 左外连接 SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column = table2.column(+);
Examples
SELECT e.last_name, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id (+) = d.department_id;
Since the connection
Example
SELECT worker.last_name || ' works for ' || manager.last_name FROM employees worker, employees manager WHERE worker.manager_id = manager.employee_id;
1999 Grammar connection
SELECT table1.column, table2.column FROM table1 [CROSS JOIN table2] | [NATURAL JOIN table2] | [JOIN table2 USING (column_name)] | [JOIN table2 ON(table1.column_name = table2.column_name)] | [LEFT|RIGHT|FULL OUTER JOIN table2 ON (table1.column_name = table2.column_name)];
ON clause to create a connection
- Natural join condition is listed as a connection with the same name of.
- ON clause can specify additional connection conditions.
- The connection condition is separate from other conditions.
- ON clause makes statements higher legibility
Examples
SELECT e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id FROM employees e JOIN departments d ON ( e.department_id = d.department_id );
ON clause to create multi-table joins
Example
SELECT employee_id, city, department_name FROM employees e JOIN departments d ON d.department_id = e.department_id JOIN locations l ON d.location_id = l.location_id;
Left outer
exemplary
SELECT e.last_name, e.department_id, d.department_name FROM employees e LEFT OUTER JOIN departments d ON ( e.department_id = d.department_id );
Right outer join
example
SELECT e.last_name, e.department_id, d.department_name FROM employees e RIGHT OUTER JOIN departments d ON ( e.department_id = d.department_id );
Full outer join
SELECT e.last_name, e.department_id, d.department_name FROM employees e FULL OUTER JOIN departments d ON ( e.department_id = d.department_id );