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

本文详细介绍了 Oracle 数据库中的 NATURAL JOIN 操作。通过实例解释了如何利用 NATURAL JOIN 自动连接具有相同名称和类型的列,并讨论了可能出现的模糊配对情况及解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

NATURAL JOIN The NATURALkeyword 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 of ANYTYPE, 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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值