Program Tip

Oracle 11g에서 + 기호를 사용하여 왼쪽 외부 조인

programtip 2020. 10. 11. 11:12
반응형

Oracle 11g에서 + 기호를 사용하여 왼쪽 외부 조인


아래 2 개의 쿼리가 Left Outer Join 또는 Right Outer Join의 예인지 말해 줄 수 있습니까 ??

Table Part:
Name         Null?       Type
PART_ID      NOT NULL    VARCHAR2(4)
SUPPLIER_ID              VARCHAR2(4)

PART_ID SUPPLIER_ID
P1      S1
P2      S2
P3  
P4  

Table Supplier:
Name            Null?     Type
SUPPLIER_ID NOT NULL      VARCHAR2(4)
SUPPLIER_NAME   NOT NULL  VARCHAR2(20)

SUPPLIER_ID  SUPPLIER_NAME
S1           Supplier#1
S2           Supplier#2
S3           Supplier#3

공급 업체의 공급 여부에 관계없이 모든 부품을 표시합니다.

P.Part_Id, S.Supplier_Name 선택
파트 P, 공급자 S에서
어디서 P.Supplier_Id = S.Supplier_Id (+)

P.Part_Id, S.Supplier_Name 선택
파트 P, 공급자 S에서
S.Supplier_Id (+) = P.Supplier_Id

TableA LEFT OUTER JOIN TableB와 동일합니다 TableB RIGHT OUTER JOIN Table A.

Oracle (+)에서 JOIN의 "옵션"테이블을 나타냅니다. 따라서 첫 번째 쿼리에서는 P LEFT OUTER JOIN S. 두 번째 쿼리에서는 S RIGHT OUTER JOIN P. 기능적으로 동일합니다.

용어에서 RIGHT 또는 LEFT는 조인의 어느쪽에 항상 레코드가 있는지 지정하고 다른 쪽은 null 일 수 있습니다. A의 그래서 P LEFT OUTER JOIN S, P그것은에 있기 때문에 항상 기록이됩니다 LEFT만, S널 (null)이 될 수 있습니다.

추가 설명 은 java2s.com의이 예제를 참조하십시오 .


명확히하기 위해 용어는 시각화를 돕기 위해 존재하기 때문에 중요하지 않다고 말하는 것 같습니다. 중요한 것은 작동 방식의 개념을 이해하는 것입니다.


오른쪽 대 왼쪽

암시 적 조인 구문에서 RIGHT와 LEFT를 결정할 때 무엇이 ​​중요한지에 대해 약간의 혼란을 보았습니다.

왼쪽 외부 조인

SELECT *
FROM A, B
WHERE A.column = B.column(+)

오른쪽 외부 조인

SELECT *
FROM A, B
WHERE B.column(+) = A.column

내가 한 것은 WHERE 절에서 용어의 측면을 바꾸는 것이지만 여전히 기능적으로 동일합니다. (그에 대한 자세한 내용은 내 대답의 위쪽을 참조하십시오.)의 배치는 (+)RIGHT 또는 LEFT를 결정합니다. (구체적 (+)으로가 오른쪽에 있으면 LEFT JOIN (+)이고 왼쪽에 있으면 RIGHT JOIN입니다.)


JOIN 유형

JOIN의 두 가지 스타일은 암시 적 JOIN명시 적 JOIN 입니다. JOIN을 작성하는 다른 스타일이지만 기능적으로는 동일합니다.

이 SO 질문을 참조하십시오 .

암시 적 조인은 단순히 모든 테이블을 함께 나열합니다. 결합 조건은 WHERE 절에 지정됩니다.

암시 적 조인

SELECT *
FROM A, B
WHERE A.column = B.column(+)

명시 적 JOIN은 조인 조건을 WHERE 절 대신 특정 테이블의 포함과 연관시킵니다.

명시 적 조인

SELECT *
FROM A
LEFT OUTER JOIN B ON A.column = B.column

이러한 암시 적 조인은 읽고 이해하기가 더 어려울 수 있으며 조인 조건이 다른 WHERE 조건에서 혼합되기 때문에 몇 가지 제한 사항이 있습니다. 따라서 암시 적 JOIN은 일반적으로 명시 적 구문을 선호하는 방식으로 권장됩니다.


이 두 쿼리가 수행되고 OUTER JOIN있습니다. 아래 참조

Oracle은 Oracle 조인 연산자보다 FROM 절 OUTER JOIN 구문을 사용하는 것이 좋습니다. Oracle 조인 연산자 (+)를 사용하는 외부 조인 쿼리에는 FROM 절 OUTER JOIN 구문에 적용되지 않는 다음 규칙 및 제한 사항이 적용됩니다.

  • FROM 절 조인 구문도 포함하는 쿼리 블록에는 (+) 연산자를 지정할 수 없습니다.

  • The (+) operator can appear only in the WHERE clause or, in the context of left- correlation (when specifying the TABLE clause) in the FROM clause, and can be applied only to a column of a table or view.

  • If A and B are joined by multiple join conditions, then you must use the (+) operator in all of these conditions. If you do not, then Oracle Database will return only the rows resulting from a simple join, but without a warning or error to advise you that you do not have the results of an outer join.

  • The (+) operator does not produce an outer join if you specify one table in the outer query and the other table in an inner query.

  • You cannot use the (+) operator to outer-join a table to itself, although self joins are valid. For example, the following statement is not valid:

    -- The following statement is not valid:
    SELECT employee_id, manager_id
       FROM employees
       WHERE employees.manager_id(+) = employees.employee_id;
    

    However, the following self join is valid:

    SELECT e1.employee_id, e1.manager_id, e2.employee_id
       FROM employees e1, employees e2
       WHERE e1.manager_id(+) = e2.employee_id
       ORDER BY e1.employee_id, e1.manager_id, e2.employee_id;
    
  • The (+) operator can be applied only to a column, not to an arbitrary expression. However, an arbitrary expression can contain one or more columns marked with the (+) operator.

  • A WHERE condition containing the (+) operator cannot be combined with another condition using the OR logical operator.

  • A WHERE condition cannot use the IN comparison condition to compare a column marked with the (+) operator with an expression.

If the WHERE clause contains a condition that compares a column from table B with a constant, then the (+) operator must be applied to the column so that Oracle returns the rows from table A for which it has generated nulls for this column. Otherwise Oracle returns only the results of a simple join.

In a query that performs outer joins of more than two pairs of tables, a single table can be the null-generated table for only one other table. For this reason, you cannot apply the (+) operator to columns of B in the join condition for A and B and the join condition for B and C. Refer to SELECT for the syntax for an outer join.

Taken from http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/queries006.htm


I saw some contradictions in the answers above, I just tried the following on Oracle 12c and the following is correct :

LEFT OUTER JOIN

SELECT *
FROM A, B
WHERE A.column = B.column(+)

RIGHT OUTER JOIN

SELECT *
FROM A, B
WHERE B.column(+) = A.column

There is some incorrect information in this thread. I copied and pasted the incorrect information:

LEFT OUTER JOIN

SELECT *
FROM A, B
WHERE A.column = B.column(+)

RIGHT OUTER JOIN

SELECT *
FROM A, B
WHERE B.column(+) = A.column

The above is WRONG!!!!! It's reversed. How I determined it's incorrect is from the following book:

Oracle OCP Introduction to Oracle 9i: SQL Exam Guide. Page 115 Table 3-1 has a good summary on this. I could not figure why my converted SQL was not working properly until I went old school and looked in a printed book!

Here is the summary from this book, copied line by line:

Oracle outer Join Syntax:

from tab_a a, tab_b b,                                       
where a.col_1 + = b.col_1                                     

ANSI/ISO Equivalent:

from tab_a a left outer join  
tab_b b on a.col_1 = b.col_1

Notice here that it's the reverse of what is posted above. I suppose it's possible for this book to have errata, however I trust this book more so than what is in this thread. It's an exam guide for crying out loud...

참고URL : https://stackoverflow.com/questions/6559261/left-outer-join-using-sign-in-oracle-11g

반응형