NATURAL JOIN--自然连接的一些注意事项

来源:互联网 发布:mac新硬盘重新安装系统 编辑:程序博客网 时间:2024/05/01 15:19

文档地址:http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10002.htm#i2080416

 

NATURAL JOIN TheNATURALkeyword indicates that a natural join is being performed. A naturaljoin is based on all columns in the two tables that have the samename. It selects rows from the two tables that have equal values inthe relevant columns. When specifying columns that are involved inthe natural join, do not qualify the column name with a table nameor table alias.

On occasion, the table pairings in natural or cross joins may beambiguous. For example:

a NATURAL LEFT JOIN b LEFT JOIN c ON b.c1 = c.c1

can be interpreted in either of the following ways:

a NATURAL LEFT JOIN (b LEFT JOIN c ON b.c1 = c.c1)    (a NATURAL LEFT JOIN b) LEFT JOIN c ON b.c1 = c.c1

To avoid this ambiguity, you can use parentheses to specify thepairings of joined tables. In the absence of such parentheses, thedatabase uses left associativity, pairing the tables from left toright.

Restriction onNatural Joins You cannot specify a LOBcolumn, columns ofANYTYPE, ANYDATA, orANYDATASET, or a collection column as part of anatural join.

 

自然连接是在两张表中寻找那些数据类型和列名都相同的字段,然后自动地将他们连接起来,
并返回所有符合条件按的结果。来看一下自然连接的例子。


Select emp.ename,dept.dname From emp natural join dept;


这里我们并没有指定连接的条件,实际上oracle为我们自作主张的将,emp中的deptno和dept
中的deptno做了连接。也就是实际上相当于


Select emp.ename,dept.dname From emp join dept on emp.deptno =dept.deptno;


因为这两张表的这两个字段deptno的类型个名称完全相同。所以使用natural join时被自然的
连接在一起了。


另外:
1.如果做自然连接的两个表的有多个字段都满足有相同的字段名称与数据类型,那么他们会被作为自然连
接的条件。


2.如果自然连接的两个表仅是字段名称相同,但数据类型不同,那么将会返回一个错误。

 

3.由于oracle中可以进行这种非常简单的naturaljoin,我们在设计表时,应该尽量在不同表
中具有相同含义的字段使用相同的名字和数据类型。以方便以后使用natural join