OCA /OCP Oracle Database 11g A ll-in-One Exam Guide- P55 potx

10 120 0
OCA /OCP Oracle Database 11g A ll-in-One Exam Guide- P55 potx

Đang tải... (xem toàn văn)

Thông tin tài liệu

OCA/OCP Oracle Database 11g All-in-One Exam Guide 496 The WHERE clause is used to specify conditions that restrict the results set of a query whether it contains joins or not. The JOIN . . . ON clause is also used to specify conditions that limit the results set created by the join. Consider the following two queries: Query 1: select d.department_name from departments d join locations l on (l.LOCATION_ID=d.LOCATION_ID) where d.department_name like 'P%'; Query 2: select d.department_name from departments d join locations l on (l.LOCATION_ID=d.LOCATION_ID and d.department_name like 'P%'); Query 1 uses a WHERE clause to restrict the 27 rows created by equijoining the DEPARTMENTS and LOCATIONS tables based on their LOCATION_ID values to the three that contain DEPARTMENT_ID values beginning with the letter “P.” Query 2 implements the condition within the brackets of the ON subclause and returns the same three rows. Five tables are joined in Figure 12-7, resulting in a list describing the top earning employees and geographical information about their departments. EXAM TIP There are three natural join formats. The pure natural join uses the NATURAL JOIN clause and joins two tables based on all columns with shared names. The other two formats use the JOIN . . . USING and JOIN . . . ON clauses and are also referred to as natural joins. They do not use the NATURAL keyword. Nonequijoins Nonequijoins match column values from different tables based on an inequality expression. The value of the join column in each row in the source table is compared to the corresponding values in the target table. A match is found if the expression used in the join, based on an inequality operator, evaluates to true. When such a join is constructed, a nonequijoin is performed. A nonequijoin is specified using the JOIN . . . ON syntax, but the join condition contains an inequality operator instead of an equal sign. The format of the syntax for a nonequijoin clause is as follows: SELECT table1.column, table2.column FROM table1 [JOIN table2 ON (table1.expr1< table2.expr2)]| [JOIN table2 ON (table1.expr1 > table2.expr2)]| [JOIN table2 ON (table1.expr1 <= table2.expr2)]| [JOIN table2 ON (table1.expr1 >= table2.expr2)]| [JOIN table2 ON (table1.expr1 BETWEEN table2.expr2 AND table2.expr3)]| [JOIN table2 ON (table1.expr1 LIKE table2. expr2)] Chapter 12: SQL Joins 497 PART II Consider the first 15 rows returned by the query in Figure 12-8. The EMPLOYEES table is nonequijoined to the JOBS table based on the inequality join condition (2*E .SALARY < J.MAX_SALARY). The JOBS table stores the salary range for different jobs in the organization. The SALARY value for each employee record is doubled and compared with all MAX_SALARY values in the JOBS table. If the join condition evaluates to true, the row is returned. The first two rows display the employee with a LAST_NAME of Abel who currently has a JOB_ID value of SA_REP and earns a SALARY of 11000. These are the only two rows in the JOBS table that satisfy the inequality join condition (2*E.SALARY < J.MAX_ SALARY) for this employee record. TIP Nonequijoins are not commonly used. The BETWEEN range operator often appears with nonequijoin conditions, since it is simpler to use one BETWEEN operator in a condition than two nonequijoin conditions based on (<=) and (>=) operators. Figure 12-7 N-way joins and additional join conditions OCA/OCP Oracle Database 11g All-in-One Exam Guide 498 Join a Table to Itself Using a Self-Join Storing hierarchical data in a single relational table is accomplished by allocating at least two columns per row. One column stores an identifier of the row’s parent record and the second stores the row’s identifier. Associating rows with each other based on a hierarchical relationship requires Oracle to self-join a table to itself. Joining a Table to Itself Using the JOIN . . . ON Clause Suppose there is a need to store a family tree in a relational table. There are several approaches one could take. One option is to use a table called FAMILY with columns named ID, NAME, MOTHER_ID, and FATHER_ID, where each row stores a person’s name, unique ID number, and the ID values for their parents. When two tables are joined, each row from the source table is subjected to the join condition with rows from the target table. If the condition evaluates to true, then the joined row, consisting of columns from both tables, is returned. When the join columns originate from the same table, a self-join is required. Conceptually, the source table is duplicated to create the target table. The self-join works like a regular join between these tables. Note that, internally, Oracle does not Figure 12-8 Nonequijoins Chapter 12: SQL Joins 499 PART II duplicate the table and this description is merely provided to explain the concept of self-joining. Consider the following three queries: Query 1: select id, name, father_id from family; Query 2: select name from family where id=&father_id; Query 3: select f1.name Dad, f2.name Child from family f1 join family f2 on (f1.id=f2.father_id) To identify someone’s father in the FAMILY table, you could use query 1 to get their ID, NAME, and FATHER_ID. In query 2, the FATHER_ID value obtained from the first query can be substituted to obtain the father’s NAME value. Notice that both queries 1 and 2 source information from the FAMILY table. Query 3 performs a self-join with the JOIN . . . ON clause by aliasing the FAMILY table as f1 and f2. Oracle treats these as different tables even though they point to the same physical table. The first occurrence of the FAMILY table, aliased as f1, is designated as the source table, while the second, aliased as f2, is assigned as the target table. The join condition in the ON clause is of the format source.child_id=target.parent_id. Figure 12-9 shows a sample of FAMILY data and demonstrates a three-way self-join to the same table. Figure 12-9 Self-join OCA/OCP Oracle Database 11g All-in-One Exam Guide 500 Exercise 12-3: Perform a Self-Join There is a hierarchical relationship between employees and their managers. For each row in the EMPLOYEES table, the MANAGER_ID column stores the EMPLOYEE_ID of every employee’s manager. Using a self-join on the EMPLOYEES table, you are required to retrieve the employee’s LAST_ NAME, EMPLOYEE_ID, manager’s LAST_NAME, and employee’s DEPARTMENT_ID for the rows with DEPARMENT_ID values of 10, 20, or 30. Alias the EMPLOYEES table as E and the second instance of the EMPLOYEES table as M. Sort the results based on the DEPARTMENT_ID column. 1. Start SQL Developer or SQL*Plus and connect to the HR schema. 2. Execute the following statement to return nine rows describing the managers of each employee in these departments: select e.last_name employee, e.employee_id, e.manager_id, m.last_name manager, e.department_id from employees e join employees m on (e.manager_id=m.employee_id) where e.department_id in (10,20,30) order by e.department_id; View Data That Does Not Meet a Join Condition by Using Outer Joins Equijoins match rows between two tables based on the equality of the terms involved in the join condition. Nonequijoins rely on matching rows between tables based on a join condition containing an inequality operator. Target table rows with no matching join column in the source table are usually not required. When they are required, however, an outer join is used to fetch them. Several variations of outer joins may be used, depending on whether join column data is missing from the source or target tables or both. These outer join techniques are described in the following topics: • Inner versus outer joins • Left outer joins • Right outer joins • Full outer joins Inner Versus Outer Joins When equijoins and nonequijoins are performed, rows from the source and target tables are matched using a join condition formulated with equality and inequality operators, respectively. These are referred to as inner joins. An outer join is performed when rows, that are not retrieved by an inner join, are returned. Two tables sometimes share a master-detail or parent-child relationship. In the HR schema the DEPARTMENTS table stores a master list of DEPARTMENT_NAME and DEPARTMENT_ID values. Each EMPLOYEES record has a DEPARTMENT_ID column constrained to be either a value that exists in the DEPARTMENTS table or null. This leads to one of the following three scenarios. The fourth scenario could occur if the constraint between the tables was removed. Chapter 12: SQL Joins 501 PART II 1. An employee row has a DEPARTMENT_ID value that matches a row in the DEPARTMENTS table. 2. An employee row has a null value in its DEPARTMENT_ID column. 3. There are rows in the DEPARTMENTS table with DEPARTMENT_ID values that are not stored in any employee records. 4. An employee row has a DEPARTMENT_ID value that is not featured in the DEPARTMENTS table. Rows matching the first scenario are retrieved using a natural inner join between the two tables. The second and third scenarios cause many problems, as these rows are excluded by inner joins. An outer join can be used to include these orphaned rows in the results set. The fourth scenario should rarely occur in a well-designed database, because foreign key constraints would prevent the insertion of child records with no parent values. Since this row will be excluded by an inner join, it may be retrieved using an outer join. A left outer join between the source and target tables returns the results of an inner join as well as rows from the source table excluded by that inner join. A right outer join between the source and target tables returns the results of an inner join as well as rows from the target table excluded by that inner join. If a join returns the results of an inner join as well as rows from both the source and target tables excluded by that inner join, then a full outer join has been performed. Left Outer Joins The format of the syntax for the LEFT OUTER JOIN clause is as follows: SELECT table1.column, table2.column FROM table1 LEFT OUTER JOIN table2 ON (table1.column = table2.column); A left outer join performs an inner join of table1 and table2 based on the condition specified after the ON keyword. Any rows from the table on the left of the JOIN keyword excluded for not fulfilling the join condition are also returned. Consider the following two queries: Query 1: select e.employee_id, e.department_id EMP_DEPT_ID, d.department_id DEPT_DEPT_ID, d.department_name from departments d left outer join employees e on (d.DEPARTMENT_ID=e.DEPARTMENT_ID) where d.department_name like 'P%'; Query 2: select e.employee_id, e.department_id EMP_DEPT_ID, d.department_id DEPT_DEPT_ID, d.department_name from departments d join employees e on (d.DEPARTMENT_ID=e.DEPARTMENT_ID) where d.department_name like 'P%'; OCA/OCP Oracle Database 11g All-in-One Exam Guide 502 Queries 1 and 2 are identical except for the join clauses, which have the keywords LEFT OUTER JOIN and JOIN, respectively. Query 2 performs an inner join and seven rows are returned. These rows share identical DEPARTMENT_ID values in both tables. Query 1 returns the same seven rows and one additional row. This extra row is obtained from the table to the left of the JOIN keyword, which is the DEPARTMENTS table. It is the row containing details of the Payroll department. The inner join does not include this row, since no employees are currently assigned to the department. A left outer join is shown in Figure 12-10. The inner join produces 27 rows with matching LOCATION_ID values in both tables. There are 43 rows in total, which implies that 16 rows were retrieved from the LOCATIONS table, which is on the left of the JOIN keyword. None of the rows from the DEPARTMENTS table contain any of these 16 LOCATION_ID values. Figure 12-10 Left outer join Chapter 12: SQL Joins 503 PART II Right Outer Joins The format of the syntax for the RIGHT OUTER JOIN clause is as follows: SELECT table1.column, table2.column FROM table1 RIGHT OUTER JOIN table2 ON (table1.column = table2.column); A right outer join performs an inner join of table1 and table2 based on the join condition specified after the ON keyword. Rows from the table to the right of the JOIN keyword, excluded by the join condition, are also returned. Consider the following query: select e.last_name, d.department_name from departments d right outer join employees e on (e.department_id=d.department_id) where e.last_name like 'G%'; The inner join produces seven rows containing details for the employees with LAST_NAME values that begin with “G.” The EMPLOYEES table is to the right of the JOIN keyword. Any employee records that do not conform to the join condition are included, provided they conform to the WHERE clause condition. In addition, the right outer join fetches one EMPLOYEE record with a LAST_NAME of Grant. This record currently has a null DEPARTMENT_ID value. The inner join excludes the record, since no DEPARTMENT_ID is assigned to this employee. A right outer join between the JOB_HISTORY and EMPLOYEES tables is shown in Figure 12-11. The EMPLOYEES table is on the right of the JOIN keyword. The DISTINCT keyword eliminates duplicate combinations of JOB_ID values from the tables. The results show the jobs that employees have historically left. The jobs that no employees have left are also returned. EXAM TIP There are three types of outer join formats. Each of them performs an inner join before including rows the join condition excluded. If a left outer join is performed, then rows excluded by the inner join, to the left of the JOIN keyword, are also returned. If a right outer join is performed, then rows excluded by the inner join, to the right of the JOIN keyword, are returned as well. Full Outer Joins The format of the syntax for the FULL OUTER JOIN clause is as follows: SELECT table1.column, table2.column FROM table1 FULL OUTER JOIN table2 ON (table1.column = table2.column); A full outer join returns the combined results of a left and right outer join. An inner join of table1 and table2 is performed before rows excluded by the join condition from both tables are merged into the results set. OCA/OCP Oracle Database 11g All-in-One Exam Guide 504 The traditional Oracle join syntax does not support a full outer join, which is typically performed by combining the results from left and right outer joins using the UNION set operator described in Chapter 13. Consider the full outer join shown in Figure 12-12. The WHERE clause restricting the results to rows with NULL DEPARTMENT_ ID values shows the orphan rows in both tables. There is one record in the EMPLOYEES table that has no DEPARTMENT_ID values, and there are 16 departments to which no employees belong. Figure 12-11 Right outer join Chapter 12: SQL Joins 505 PART II Generate a Cartesian Product of Two or More Tables A Cartesian product of two tables is created by joining each row of the source table with every row in the target table. The number of rows in the result set created by a Cartesian product is equal to the number of rows in the source table multiplied by the number of rows in the target table. Cartesian products may be formed intentionally using the ANSI SQL:1999 cross join syntax. This technique is described in the next section. Figure 12-12 Full outer join . conditions based on (<=) and (>=) operators. Figure 12-7 N-way joins and additional join conditions OCA/ OCP Oracle Database 11g All-in-One Exam Guide 498 Join a Table to Itself Using a Self-Join Storing. demonstrates a three-way self-join to the same table. Figure 12-9 Self-join OCA/ OCP Oracle Database 11g All-in-One Exam Guide 500 Exercise 12-3: Perform a Self-Join There is a hierarchical relationship. d.department_name like 'P%'; OCA/ OCP Oracle Database 11g All-in-One Exam Guide 502 Queries 1 and 2 are identical except for the join clauses, which have the keywords LEFT OUTER JOIN and

Ngày đăng: 06/07/2014, 13:20

Mục lục

  • Contents

  • Introduction

  • Part I: Oracle Database 11g Administration

    • Chapter 1 Architectural Overview of Oracle Database 11g

      • Exam Objectives

      • Oracle Product Stack

      • Prerequisite Concepts

      • Single-Instance Architecture

      • Instance Memory Structures

      • Instance Process Structures

      • Database Storage Structures

      • Two-Minute Drill

      • Self Test

      • Self Test Answers

      • Chapter 2 Installing and Creating a Database

        • Exam Objectives

        • Identify the Tools for Administering an Oracle Database

        • Plan an Oracle Database Installation

        • Install the Oracle Software by Using the Oracle Universal Installer (OUI)

        • Create a Database by Using the Database Configuration Assistant

        • Two-Minute Drill

        • Self Test

        • Self Test Answers

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan