Oracle multi-Table connection

Source: Internet
Author: User
1. internal Connections (natural connections) 2. outer Join (1) left Outer Join (no restrictions on the table on the left) (2) Right Outer Join (no restrictions on the table on the right) (3) full outer join (no restrictions are imposed on both the left and right tables) 3. standard SQL Syntax: selecttable1.column, table2.columnfromtable1 [inner | left

1. internal Connections (natural connections) 2. outer Join (1) left Outer Join (no restrictions on the table on the left) (2) Right Outer Join (no restrictions on the table on the right) (3) full outer join (no restrictions are imposed on both the left and right tables) 3. standard SQL syntax for self-join (join in the same table): select table1. column, table2. column from table1 [inner | left

1. inner connection (natural connection)

2. External Connection

(1) left Outer Join (the table on the left is not restricted)

(2) Right Outer Join (the table on the right is not restricted)

(3) Full outer join (no restrictions are imposed on both the left and right tables)

3. Self-join (connections in the same table)

Standard SQL Syntax:

 
 
  1. select table1.column,table2.column
  2. from table1 [inner | left | right | full ] join table2 on table1.column1 = table2.column2;

Inner join indicates the inner join;
Left join indicates the left Outer join;
Right join indicates the right outer join;
Full join indicates a full outer join;
The on clause is used to specify the connection conditions.

Note:

If you use the from clause to specify the internal and external connections, you must use the on clause to specify the connection conditions;

If you use the (+) operator to specify the outer join, you must use the where clause to specify the join condition.

1. Inner Join/Join)

1.1 Inner Join

The Inner join logical operator returns each row that matches the first (top) input and the second (bottom) Input join. This is the same as querying multiple tables using select statements, so there are very few internal connections.
Another note is that Join is an inner join by default. Therefore, when writing an inner connection, we can omit the keyword inner.

1.2 The following is an example to describe the internal connection:

1.2.1 create two test tables and insert data first:

 
 
  1. SQL> select * from dave;
  2. ID NAME
  3. ---------- ----------
  4. 1 dave
  5. 2 bl
  6. 1 bl
  7. 2 dave
  8. SQL> select * from bl;
  9. ID NAME
  10. ---------- ----------
  11. 1 dave
  12. 2 bl

1.2.3 use internal links for query:

 
 
  1. SQL> Select a. id, a. name, B. name from dave a inner join bl B on a. id = B. id; -- standard syntax
  2. ID NAME
  3. ------------------------------
  4. 1 dave
  5. 2 bl
  6. 1 bl dave
  7. 2 dave bl
  8. SQL> Select a. id, a. name, B. name from dave a join bl B on a. id = B. id; -- the inner keyword is omitted here.
  9. ID NAME
  10. ------------------------------
  11. 1 dave
  12. 2 bl
  13. 1 bl dave
  14. 2 dave bl
  15. SQL> Select a. id, a. name, B. name from dave a, bl B where a. id = B. id; -- select multi-Table query
  16. ID NAME
  17. ------------------------------
  18. 1 dave
  19. 2 bl
  20. 1 bl dave
  21. 2 dave bl

From the results of these three SQL statements, we can also see that their functions are the same.

1.3 Natural join)

A natural join is to search for fields with the same data type and column name in the two tables, then automatically connect them, and return all results that meet the condition.

Let's take a look at the examples of natural connections:

 
 
  1. SQL> Select id,name from dave a natural join bl b;
  2. ID NAME
  3. ---------- ----------
  4. 1 dave
  5. 2 bl

Here we do not specify the connection conditions. In fact, oracle makes our own claim to connect the id and name fields in the dave table with the id and name fields in the bl table. That is, it is actually equivalent

 
 
  1. SQL> Select dave.id,bl.name
  2. From dave join bl on dave.id = bl.id and dave.name=bl.name;
  3. ID NAME
  4. ---------- ----------
  5. 1 dave
  6. 2 bl

Therefore, we can also regard natural connections as an internal connection.

Notes for natural connection:

(1). If multiple fields in the two tables that are naturally connected meet the same name and type, they will be used as conditions for natural connection.

(2) If the two tables that are naturally connected have the same field name but different data types, an error is returned.

Ii. Outer Join)

Outer join returns each row that matches the join of the first (top) input and the second (bottom) input. It also returns any rows in the first input without matching rows in the second input. There are three types of external connections: left Outer Join, right outer join, and full outer join. Corresponding SQL: LEFT/RIGHT/FULL OUTER JOIN. Generally, the keyword outer is omitted. Written as LEFT/RIGHT/full join.

When connecting left Outer Join and right Outer Join, a table is used as the base table, and the table content is displayed in full, and the matching content of the two tables is added. If the data in the base table is not recorded in the other table. The column in the row of the associated result set is displayed as NULL ).

You can also use "(+)" for external connections. Notes for using (+:

1. the (+) operator can only appear in the where clause and cannot be used with the outer join syntax.

2. When an external join is executed using the (+) operator, if the where clause contains multiple conditions, the (+) operator must be included in all conditions.

3. the (+) operator is only applicable to columns and cannot be used in expressions.

4. the (+) operator cannot be used with the or and in operators.

5. the (+) operator can only be used to implement left Outer Join and right outer join, but not to implement full outer join.

Before doing the experiment, add some different data to the dave table and bl. To facilitate testing.

 
 
  1. SQL> select * from bl;
  2. ID NAME
  3. --------------------
  4. 1 dave
  5. 2 bl
  6. 3 big bird
  7. 4 exc
  8. 9 Huai Ning
  9. SQL> select * from dave;
  10. ID NAME
  11. --------------------
  12. 8 Anqing
  13. 1 dave
  14. 2 bl
  15. 1 bl
  16. 2 dave
  17. 3 dba
  18. 4 sf-express
  19. 5 dmm

2.1 Left outer join (left outer join/Left join)

Left join is based on records in the left table. In this example, Dave can be viewed as the left table, BL can be viewed as the right table, and its result set is the data in Dave's table, add data that matches the Dave table and the BL table. In other words, all records in the left table (Dave) are displayed, while the right table (BL) only displays records that meet the search criteria. All records in the BL table are NULL.

Example:

 
 
  1. SQL> select * from dave a left join bl B on a. id = B. id;
  2. ID NAME
  3. ---------------------------------------
  4. 1 bl 1 dave
  5. 1 dave 1 dave
  6. 2 dave 2 bl
  7. 2 bl 2 bl
  8. 3 dba 3 big bird
  9. 4 sf-express 4 exc
  10. 5 dmm -- here, table B is null because it does not match
  11. 8 Anqing -- table B is null because it does not match
  12. SQL> select * from dave a left outer join bl B on a. id = B. id;
  13. ID NAME
  14. ----------------------------------------
  15. 1 bl 1 dave
  16. 1 dave 1 dave
  17. 2 dave 2 bl
  18. 2 bl 2 bl
  19. 3 dba 3 big bird
  20. 4 sf-express 4 exc
  21. 5 dmm
  22. 8 Anqing

This number can be understood as follows: + indicates the supplement, that is, which table has a plus sign, and this table is a matching table. Therefore, the plus sign is written in the right table, and the left table is all displayed, so it is a left join.

 
 
  1. SQL> Select * from dave a, bl B where a. id = B. id (+); -- Note: The where keyword is used when (+) is used.
  2. ID NAME
  3. ----------------------------------------
  4. 1 bl 1 dave
  5. 1 dave 1 dave
  6. 2 dave 2 bl
  7. 2 bl 2 bl
  8. 3 dba 3 big bird
  9. 4 sf-express 4 exc
  10. 5 dmm
  11. 8 Anqing

2.2 right outer join (right outer join/right join)

The result of left join is the opposite. It is based on the right table (BL) and displays the records of the BL table. The results of Dave and BL matching are added. Fill the Dave table with NULL when the table is insufficient.

Example:

 
 
  1. SQL> select * from dave a right join bl B on a. id = B. id;
  2. ID NAME
  3. ----------------------------------------
  4. 1 dave 1 dave
  5. 2 bl 2 bl
  6. 1 bl 1 dave
  7. 2 dave 2 bl
  8. 3 dba 3 big bird
  9. 4 sf-express 4 exc
  10. 9 Huai Ning -- Null is not enough in the left table.

You have selected 7 rows.

 
 
  1. SQL> select * from dave a right outer join bl B on a. id = B. id;
  2. ID NAME
  3. ----------------------------------------
  4. 1 dave 1 dave
  5. 2 bl 2 bl
  6. 1 bl 1 dave
  7. 2 dave 2 bl
  8. 3 dba 3 big bird
  9. 4 sf-express 4 exc
  10. 9 Huai Ning -- Null is not enough in the left table.

You have selected 7 rows.

This number can be understood as follows: + indicates the supplement, that is, which table has a plus sign, and this table is a matching table. Therefore, the plus sign is written in the left table, and the right table is all displayed, so it is a right join.

 
 
  1. SQL> Select * from dave a, bl B where a. id (+) = B. id;
  2. ID NAME
  3. ----------------------------------------
  4. 1 dave 1 dave
  5. 2 bl 2 bl
  6. 1 bl 1 dave
  7. 2 dave 2 bl
  8. 3 dba 3 big bird
  9. 4 sf-express 4 exc
  10. 9 Huai Ning

2.3 full outer join (full outer join/full join)

No restrictions are imposed on the left and right tables. All records are displayed. If the two tables are insufficient, use null to fill them. All external connections do not support (+.

Example:

 
 
  1. SQL> select * from dave a full join bl B on a. id = B. id;
  2. ID NAME
  3. ----------------------------------------
  4. 8 Anqing
  5. 1 dave 1 dave
  6. 2 bl 2 bl
  7. 1 bl 1 dave
  8. 2 dave 2 bl
  9. 3 dba 3 big bird
  10. 4 sf-express 4 exc
  11. 5 dmm
  12. 9 Huai Ning

Nine rows have been selected.

 
 
  1. SQL> select * from dave a full outer join bl B on a. id = B. id;
  2. ID NAME
  3. ----------------------------------------
  4. 8 Anqing
  5. 1 dave 1 dave
  6. 2 bl 2 bl
  7. 1 bl 1 dave
  8. 2 dave 2 bl
  9. 3 dba 3 big bird
  10. 4 sf-express 4 exc
  11. 5 dmm
  12. 9 Huai Ning

Nine rows have been selected.

Iii. Self-connection

Self join is a commonly used connection method in SQL statements. Using self join, you can treat one image of your table as another table, so that you can get some special data.

Example:

One table in oracle scott's schema is emp. Every employee in emp has its own mgr (manager), and each manager is also an employee of the company, and also has its own manager.

Next we need to find out the names of employees and managers. What should we do at this time?

If we have two such tables to teach worker and mgr respectively, we can write SQL statements very well.

Select worker. name,

Mgr. name

From worker, mgr

Where worker. id = mgr. id;

But now we only have one emp table. So we can use self-connection. The intention of Self-join is to regard a table as multiple tables for join. We can write SQL statements like this:

 
 
  1. SQL> select work. ename worker, mgr. ename manager from scott. emp work, scott. emp mgr
  2. 2 where work. mgr = mgr. empno (+)
  3. 3 order by work. ename;
  4. WORKER MANAGER
  5. --------------------
  6. ADAMS SCOTT
  7. ALLEN BLAKE
  8. BLAKE KING
  9. CLARK KING
  10. FORD JONES
  11. JAMES BLAKE
  12. JONES KING
  13. KING -- the right table is not filled with Null.
  14. MARTIN BLAKE
  15. MILLER CLARK
  16. SCOTT JONES
  17. SMITH FORD
  18. TURNER BLAKE
  19. WARD BLAKE

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.