oracle中的内连接
我正在考虑 Oracle SQL 实现中内连接的语法,这里似乎有点不一致:
假设您有两个关系贷款(贷款号,分支名称,金额)和借款人(客户名称,贷款号)。 Loan_number 是两个表共有的属性。现在,Oracle 为您提供了两种表达内连接的方法:
select *
from loan, borrower
where loan.loan_number = borrower.loan_number;
上面的语句等效于:
select *
from loan
inner join borrower
on loan.loan_number = borrower.loan_number;
但是,当表达交叉连接时,只有一种表达方法:
select *
from loan, borrower;
下面的语句在语法上是不正确的:
select *
from loan
inner join borrower;
这是无效的; Oracle 期望子句中的 ON... 部分
鉴于内连接只是带有过滤条件的交叉连接,你们认为这是 Oracle SQL 实现中的不一致吗?我错过了什么吗? 我有兴趣听听其他一些意见。谢谢。
正如大卫在他的回答中指出的语法是:
select *
from loan cross join borrower;
即使我不知道上述语法,我仍然认为它是不一致的。除了允许没有连接条件的内部连接之外,还可以使用交叉连接关键字。交叉连接实际上是没有连接条件的内连接,为什么不将其表示为没有连接条件的内连接呢?
I was thinking about the syntax of inner joins in Oracle's SQL implementation and here is something that seems a bit inconsistent:
Let's say you have two relations loan(loan_number, branch_name, amount) and borrower(customer_name, loan_number). loan_number is the attribute common to both tables. Now, Oracle gives you two ways to express an inner join:
select *
from loan, borrower
where loan.loan_number = borrower.loan_number;
The above statement is equivalent to:
select *
from loan
inner join borrower
on loan.loan_number = borrower.loan_number;
However, when expressing a cross join there is only one way to express it:
select *
from loan, borrower;
The following statement is syntactically incorrect:
select *
from loan
inner join borrower;
This is invalid; Oracle expects the ON... part of the clause
Given that an inner join is just a cross join with a filter condition, do you guys think that this is an inconsistency in Oracle's SQL implementation? Am I missing something?
I'd be interested in hearing some other opinions. Thanks.
As David pointed out in his answer the syntax is:
select *
from loan cross join borrower;
Even though I was not aware of the above syntax I still think it's inconsistent. Having the cross join keyword in addition to allowing inner join without a join condition would be fine. A cross join is in fact an inner join without a join condition, why not express it as an inner join without the join condition?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这种表达内连接的方式:
近 20 年来都不推荐。它被保留是因为它只是一个恰好传达内部连接的有效表达式。我会集中精力使用更接近当前标准的版本,最大限度地减少误解和错误的可能性。
This way of expressing inner joins:
is not recommended for almost 20 years. It was kept because it is simply a valid expression that happens to convey an inner join. I would concentrate in using the version closer to the current standard, minimizing the chances for misunderstanding and flat out errors.
我同意这并不一致。
但我认为 Oracle 实现是一件好事:
ON
部分是强制性的。对我来说,不保持 100% 一致很有意义——这有助于避免错误。
I would agree that it is not consistent.
But I would argue that the Oracle implementation is a good thing:
ON
part is mandatory.CROSS JOIN
sytax.Makes a lot of sense to me not to be 100% consistent - it helps to avoid you mistakes.
没有不一致之处。
No inconsistency.
Oracle 还支持自然连接语法,该语法根据共享列名连接两个表。这适用于您的情况,因为两个表都有一个名为 LOAN_NUMBER 的列。
现在,在这种情况下可以提出相同的论点,即完全没有必要使用关键字natural。但是,如果我们遵循逻辑,我们最终会遇到这样一种情况:该语句可能是交叉连接,也可能是自然连接,具体取决于列名称:
这显然是不可取的,因为将 LOAN.LOAN_NUMBER 重命名为 LOAN_ID 会更改结果集。
所以,这就是你的答案:消歧义。
Oracle also supports the natural join syntax, which joins two tables on the basis of shared column name(s). This would work in your case because both tables have a column called LOAN_NUMBER.
Now, your same argument could be made in this case, that the use of the keyword natural is strictly unnecessary. But if we follow the logic we end up with a situation in which this statement could be either a cross join or a natural join, depending on the column names:
This is clearly undesirable, if only because renaming LOAN.LOAN_NUMBER to LOAN_ID would change the result set.
So, there's your answer: disambiguation.