当我们想要的数据来自于多余一个table的时候我们需要合并表格,任何两个我们想要合并的tabel,必须有一下3种关系之一: one-to-one,one-to-many,many-to-many. 简单来说, one-to-one 是指一个table中一个column和另一个table的column相同。
表格合并分为两类:equi join和 theta join(non-equi join).
equi join
比如下面的一个例子,实际上很多合并时equi join.
Natural join
举个例子如下:
就是会去掉重复的元素从而合并。
Theta join
Outer join
outer join 是特殊的equi join,nUsually, only a subset of tuples of each relation will actually participate in a join, i.e. only tuples that match with the joining attributes. Tuples of a relation not participating in a join are called dangling tuples.
There are three types of outer joins
left outer join right outer join and full outer join.
另外一个经常忽略的一点是:
如果两个表格中分别存在相同的column.如果是many-to many relation ,若下面的情况。
EID |
ENA |
MGR |
DNO |
1 |
SALLY |
3 |
1 |
2 |
TIM |
3 |
1 |
3 |
MARY |
|
|
DNO |
DNAME |
MGR |
1 |
PROD |
3 |
|
|
|
如上两个表格中分别两个columns是相同的,如果要用natural join 则系统会判别不出来,因此要用一个 using 这样一个语句来辨别.
SELECT ENAME,DNAME
FROM EMP EQUI JOIN DEPT
USING(DNO)
WHERE DEPTNO IN(10,40,60);