多表连接中的各种连接的区别和联系
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