多表連接中的各種連接的區別和聯系
2024-07-21 02:05:54
供稿:網友
 
知識點:
      1、各種連接的定義和用途
          內連接:
                 同等連接、非同等連接(常用就不講了)
                 自然連接,自己找相同的字段,不用給出兩個表之間拿哪一列進行比較。所謂自然(不常用)
                 交叉連接,選出來之后,進行笛卡乘積。如果要列出所有的組合時,這個有用。(不常用)
          外連接:
                 左右外連接 (常用就不講了)
                 全外連接 full join(不常用)。
                  合并連接(一般不用),是選出不匹配的記錄  等于全連接減去內連接的集合。
      2、關鍵是弄清楚它們之間的區別。
                 1)full outer join 和cross join 有什么區別?
                   答:交叉連接是屬于內連接中的一種特殊的連接。根本不存在任何條件連接,應該說是范圍最大的連接。就是
                       笛卡爾乘積。   而 全外連接是首先進行內連接,然后將所有沒匹配上的記錄也選出來,但不在對不匹配的進行笛卡爾乘積
                       只是加進來。  而交叉連接是會將沒匹配上的也要進行乘積的。
相關題目:
you are a database developer for an automobile dealership. you are designing a database to support a  
web site that will be used for purchasing automobiles. a person purchasing an automobile from the web  
site will be able to customize his or her order by selecting the model and color.  
the manufacturer makes four different models of automobiles. the models can be ordered in any one of  
five colors. a default color is assigned to each model.  
the models are stored in a table named models, and the colors are stored in a table named colors. these  
tables are shown in the exhibit.  
you need to create a list of all possible model and color combinations. which script should you use? 
 
 (分析,這里是要列出車型和顏色的所有組合,而根本不涉及條件,)
   a            select m.modelname, c.colorname  
from colors as c full outer join models as m  
   on c.colorid = m.colorid  
order by m.modelname, c.colorname  (分析,這里是全外連接,因為車型表里只有一個顏色id(缺省),那么這里就只有8(4+4)條記錄選出來了)
 
   b            select m.modelname, c.colorname   
from colors as c cross join models as m  
order by m.modelname, c.colorname       (答案在此,交叉連接就是列出所有的可能組合20(5*4)條記錄,根本不用條件) 
 
   c            select m.modelname, c.colorname  
from colors as m inner join colors as c  
on m.colorid = c.colorid  
order by m.modelname, c.colorname  (分析,內連接,缺省的情況下只能查出4條記錄) 
 
   d            select m.modelname, c.colorname  
from colors as c left outer join models as m (分析,左外連接,缺省情況下只能查出5條記錄)  
on c.colorid = m.colorid  
union  
select m.modelname, c.colorname  
from colors as c right outer join models as m  (分析,右外連接,缺省情況下只能查處4條記錄) 
on c.colorid = m.colorid  
order by m.modelname, c.colorname 
 
   e            select m.modelname  
from models as m  
union  
select c.colorname     (分析,這個是合并連接,是查詢不匹配的行。 應該是   8-4=4 條記錄,就是車型表里不存在的顏色)  
from colors as c  
order by m.modelname