知识点:
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