oracle中的内连接

发布于 2024-08-11 21:28:02 字数 883 浏览 6 评论 0原文

我正在考虑 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

狼亦尘 2024-08-18 21:28:03

这种表达内连接的方式:

select * from loan, borrower where loan.loan_number = borrower.loan_number;

近 20 年来都不推荐。它被保留是因为它只是一个恰好传达内部连接的有效表达式。我会集中精力使用更接近当前标准的版本,最大限度地减少误解和错误的可能性。

This way of expressing inner joins:

select * from loan, borrower where loan.loan_number = borrower.loan_number;

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.

同尘 2024-08-18 21:28:02

我同意这并不一致。

但我认为 Oracle 实现是一件好事:

  • 当您进行联接时,您几乎总是希望包含过滤条件,因此 ON 部分是强制性的。
  • 如果您真的非常不想有过滤条件(您真的确定吗?),您必须使用 CROSS JOIN 语法显式告诉 Oracle。

对我来说,不保持 100% 一致很有意义——这有助于避免错误。

I would agree that it is not consistent.

But I would argue that the Oracle implementation is a good thing:

  • when you do a join, you almost always want to include a filter condition, therefore the ON part is mandatory.
  • If you really, really don't want to have a filter condition (are you really sure?), you have to tell Oracle explicitly with CROSS JOIN sytax.

Makes a lot of sense to me not to be 100% consistent - it helps to avoid you mistakes.

晚雾 2024-08-18 21:28:02

SELECT *
FROM Loan
CROSS JOIN Borrower

没有不一致之处。


SELECT *
FROM Loan
CROSS JOIN Borrower

No inconsistency.

亚希 2024-08-18 21:28:02

Oracle 还支持自然连接语法,该语法根据共享列名连接两个表。这适用于您的情况,因为两个表都有一个名为 LOAN_NUMBER 的列。

SELECT *
FROM Loan
NATURAL JOIN Borrower

现在,在这种情况下可以提出相同的论点,即完全没有必要使用关键字natural。但是,如果我们遵循逻辑,我们最终会遇到这样一种情况:该语句可能是交叉连接,也可能是自然连接,具体取决于列名称:

SELECT *
FROM Loan
JOIN Borrower

这显然是不可取的,因为将 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.

SELECT *
FROM Loan
NATURAL JOIN Borrower

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:

SELECT *
FROM Loan
JOIN Borrower

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文