当前位置:网站首页>Oracle advanced (IV) table connection explanation

Oracle advanced (IV) table connection explanation

2022-07-06 18:45:00 InfoQ

One 、 Preface

In the course of work , Often check SQL sentence , There are some SQL The statement is extremely complex , When it comes to multi table operations , Table join statements are the most common . For example, internal connection 、 External connection 、 Self join .

Oracle  There are three types of connections between tables :

  • Internal connection ( Natural join )
  • External connection (1) The left outer join  ( The table on the left is unrestricted )(2) Right connection ( The table on the right is unrestricted )(3) Full outer join ( There are no restrictions on the left and right tables )
  • Self join ( Connections in the same table )

SQL The standard grammar of :

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

among ,

  • inner join
      Indicates internal connection ;
  • left join
    Left outer connection ;
  • right join
    Right outer connection ;
  • full join
    Indicates full external connection ;
  • on
    Clause is used to specify the connection condition .

Be careful :

  • If you use
    from
    Clause specifies the inside 、 External connection , You have to use
    on
    Clause specifies the connection condition ;
  • If you use (
    +
    ) Operator specifies the outer join , Must be used
    where
    Clause specifies the connection condition .

Two 、 Internal connection (Inner Join/Join)

2.1 Inner Join

Inner join
The logical operator returns the first ( Apex ) Enter with the second ( Bottom ) Enter each row of the join . This is the same thing select Querying multiple tables has the same effect , So internal connections are rarely used .

Another point to note is Join  The default is inner join.  So we can omit... When writing inner connections inner  This keyword .

2.2  Illustrate with examples

1.2.1  First create 2 Test table and insert data :

SQL> select * from dave;
ID NAME
1 dave
2 bl
1 bl
2 dave
SQL> select * from bl;
ID NAME
1 dave
2 bl

1.2.3  Query with internal links :

SQL> Select a.id,a.name,b.name from dave a inner join bl b on a.id=b.id; --  Standard writing
ID NAME NAME
1 dave dave
2 bl bl
1 bl dave
2 dave bl
SQL> Select a.id,a.name,b.name from dave a join bl b on a.id=b.id; --  Omit here inner  keyword
ID NAME NAME
1 dave dave
2 bl bl
1 bl dave
2 dave bl
SQL> Select a.id,a.name,b.name from dave a,bl b where a.id=b.id; -- select  Multi-table query
ID NAME NAME
1 dave dave
2 bl bl
1 bl dave
2 dave bl

From these three SQL  We can also see the results of , Their role is the same .

2.3  Natural join (Natural join)

Natural join is to look for fields with the same data type and column name in two tables , Then automatically connect them , And return all qualified results .

Let's take a look at the example of natural connection :

SQL> Select id,name from dave a natural join bl b; 
ID NAME
1 dave
2 bl

Here we do not specify the connection conditions , actually oracle For our own sake will ,dave In the table id and name Fields and bl In the table id and name Fields are connected . Which is actually equivalent to

SQL> Select dave.id,bl.name From dave join bl on dave.id = bl.id and dave.name=bl.name;
ID NAME
1 dave
2 bl

therefore , We can also understand natural connection as a kind of inner connection . Some considerations about natural connections :

  • If there are multiple fields of two tables with natural connection that have the same name and type , Then they will be regarded as the condition of natural connection .
  • If two naturally connected tables only have the same field name , But the data types are different , Then an error will be returned .

3、 ... and 、 External connection (Outer Join)

outer join Will return the first one that satisfies each ( Apex ) Enter with the second ( Bottom ) Enter the row of the join . It also returns any line in the first input that does not have a matching line in the second input . There are three types of external connections :  The left outer join , Right connection , Full outer join .  Corresponding SQL:LEFT/RIGHT/FULL OUTER JOIN.  Usually we omit outer  This keyword .  It's written in :LEFT/RIGHT/FULL JOIN.

In both the left outer join and the right outer join, a table will be used as the base table , The contents of the table will be displayed in full , Then add the matching contents of the two tables .  If the data of the base table is not recorded in another table .  Then the columns in the associated result set row are displayed as null values (NULL).

For external connections ,  You can also use “(+) ” To express .  About use (+) Some do's and don 'ts :

  • (+) Operators can only appear in where clause , And not with outer join Grammar is used at the same time .
  • When using (+) When the operator performs an outer join , If in where Clause contains multiple conditions , Must include... In all conditions (+) The operator
  • (+) The operator applies only to columns , It can't be used in expressions .
  • (+) Operators cannot be associated with or and in Operators are used together .
  • (+) Operators can only be used to implement left outer join and right outer join , It cannot be used to realize full external connection .

(+) The operator applies only to columns , It can't be used in expressions . Before doing the experiment , We will first dave Table and bl Add some different data .  To facilitate testing .

SQL> select * from bl;
 ID NAME
 1 dave
 2 bl
 3 big bird
 4 exc
 9  Huaining
SQL> select * from dave;
 ID NAME
 8  Anqing
 1 dave
 2 bl
 1 bl
 2 dave
 3 dba
 4 sf-express
 5 dmm

3.1  The left outer join (Left outer join/ left join)

left join
It is based on the records in the left table , Example Dave It can be seen as the left table ,BL It can be seen as the right table , The result set is zero Dave Table data , Plus Dave Table and BL Table matching data . let me put it another way , The left table (Dave) All of the records will be represented , The right table (BL) Only records that match the search criteria are displayed .BL The insufficient records in the table are NULL.

Example :

SQL> select * from dave a left join bl b on a.id = b.id;
 ID NAME ID NAME
 1 bl 1 dave
 1 dave 1 dave
 2 dave 2 bl
 2 bl 2 bl
 3 dba 3 big bird
 4 sf-express 4 exc
 5 dmm --  here B The table is null, Because there is no match to
 8  Anqing  --  here B The table is null, Because there is no match to

SQL> select * from dave a left outer join bl b on a.id = b.id;
 ID NAME ID NAME
 1 bl 1 dave
 1 dave 1 dave
 2 dave 2 bl
 2 bl 2 bl
 3 dba 3 big bird
 4 sf-express 4 exc
 5 dmm
 8  Anqing

use (
+
) To achieve , This
+
It can be understood in this way : 
+
  Express supplement , Which table has a plus sign , This table is the matching table . So the plus sign is in the right table , The left table shows all , So it's left connection .

SQL> Select * from dave a,bl b where a.id=b.id(+); --  Be careful :  use (+)  You have to use keywords where
 ID NAME ID NAME
 1 bl 1 dave
 1 dave 1 dave
 2 dave 2 bl
 2 bl 2 bl
 3 dba 3 big bird
 4 sf-express 4 exc
 5 dmm
 8  Anqing

3.2  Right connection (right outer join/ right join)

and
left join
It turns out the opposite , Right table (BL) On the basis of ,  Show BL All the records of the table , On the plus Dave and BL  Matching results . Dave Where the watch is not enough NULL fill . Example :

SQL> select * from dave a right join bl b on a.id = b.id;
 ID NAME ID NAME
 1 dave 1 dave
 2 bl 2 bl
 1 bl 1 dave
 2 dave 2 bl
 3 dba 3 big bird
 4 sf-express 4 exc
 9  Huaining  -- The left table here is not enough Null  fill

-- Have chosen 7 That's ok .
SQL> select * from dave a right outer join bl b on a.id = b.id;
 ID NAME ID NAME
 1 dave 1 dave
 2 bl 2 bl
 1 bl 1 dave
 2 dave 2 bl
 3 dba 3 big bird
 4 sf-express 4 exc
 9  Huaining  -- The left table here is not enough Null  fill
-- Have chosen 7 That's ok .

use (
+
) To achieve ,  This + It can be understood in this way : 
+
  Express supplement , Which table has a plus sign , This table is the matching table . So the plus sign is on the left , The right table shows all , So right connection .

SQL> Select * from dave a,bl b where a.id(+)=b.id;
 ID NAME ID NAME
 1 dave 1 dave
 2 bl 2 bl
 1 bl 1 dave
 2 dave 2 bl
 3 dba 3 big bird
 4 sf-express 4 exc
 9  Huaining

3.3  Full outer join (full outer join/ full join)

There are no restrictions on the left and right tables , All the records show , The two meters are not enough
null
  fill .  All external connections do not support (
+
) This kind of writing . Example :

SQL> select * from dave a full join bl b on a.id = b.id;
 ID NAME ID NAME
 8  Anqing
 1 dave 1 dave
 2 bl 2 bl
 1 bl 1 dave
 2 dave 2 bl
 3 dba 3 big bird
 4 sf-express 4 exc
 5 dmm
 9  Huaining

-- Have chosen 9 That's ok .
SQL> select * from dave a full outer join bl b on a.id = b.id;
 ID NAME ID NAME
 8  Anqing
 1 dave 1 dave
 2 bl 2 bl
 1 bl 1 dave
 2 dave 2 bl
 3 dba 3 big bird
 4 sf-express 4 exc
 5 dmm
 9  Huaining

Have chosen 9 That's ok .

Four 、 Self join

Self join (
self join
) yes SQL The connection method often used in statements , Using self join, you can treat an image of your own table as another table , So we can get some special data .

Example : stay
oracle
Of scott Of schema One of the tables in is emp. stay emp Every employee in has his own mgr( The manager ), And every manager is also an employee of the company , I have my own manager .

Next, we need to find out each employee's own name and the manager's name . What should we do at this time ?

If we have two such tables to teach worker and mgr, Then we can write SQL sentence .

Select worker.name,Mgr.name From worker,mgr Where worker.id = mgr.id; 

But now there is only one emp surface . Therefore, self connection can be adopted . The original meaning of self join is to connect a table as multiple tables . You can write like this SQL sentence :

select work.ename worker,mgr.ename manager from scott.emp work, scott.emp mgr where work.mgr = mgr.empno order by work.ename;

WORKER MANAGER
ADAMS SCOTT
ALLEN BLAKE
BLAKE KING
CLARK KING
FORD JONES
JAMES BLAKE
JONES KING
MARTIN BLAKE
MILLER CLARK
SCOTT JONES
SMITH FORD
WORKER MANAGER
TURNER BLAKE
WARD BLAKE

Have chosen 13 That's ok

5、 ... and 、 Connection diagram

null
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061040510941.html