有没有一种方法可以使“自然加入”还有一张桌子上有外键吗?

发布于 2025-01-25 10:46:33 字数 807 浏览 0 评论 0原文

这只是一个虚拟的例子。 实际上,我有一个外键,它引用了更多的列。这就是为什么我尝试用“自然连接”替换普通加入的原因,

我有一张桌子上有另一个桌子上的外键。 这些列没有相同的名称。

我想避免写入this_column_of_tablea = this_column_of_tableB

,如果表格的外键具有引用列的相同名称,我可以自然地加入。

尽管如此,我还是尝试过。正如预期的那样,它无效。 (跨产品),

但是Oracle应该知道要使用哪个列来加入。因为它是表定义。

在这种情况下,这是有可能制作一个联合的,而无需重写哪个列匹配哪个列。这很容易犯错误和浪费时间。

create table TB (  
  TB_1          number,  
  constraint fk_TA foreign key (TB_1)
  REFERENCES TA(TA_1)
);

create table TA (  
  TA_1          number,  
  constraint pk_departments primary key (TA_1)  
);

INSERT INTO TA (TA_1)
   VALUES (1);
   
INSERT INTO TA (TA_1)
   VALUES (2);
   
INSERT INTO TA (TA_1)
   VALUES (3);
   
INSERT INTO TB(TB_1)
   VALUES (1);

INSERT INTO TB(TB_1)
   VALUES (2);
   
select * from TA natural join TB

This is just a dummy example.
In reality I have a foreign key which references a lot of more columns. That why I'm trying replace the normal join with a "natural join"

I have a table which has a foreign key on another table.
The columns don't have the same name.

I would like to avoid writing join on this_column_of_tableA= this_column_of_tableB

If the foreign key of tableB had the same name of the referencing column, I could do a natural join.

I have tried nonetheless. As expected it didn't work. (cross product)

But Oracle should know which column to use to make the join. Because it is the table definition.

Is this possible to make a jointure in this case without rewriting which column matchs which column. It's prone making errors and a waste of time.

create table TB (  
  TB_1          number,  
  constraint fk_TA foreign key (TB_1)
  REFERENCES TA(TA_1)
);

create table TA (  
  TA_1          number,  
  constraint pk_departments primary key (TA_1)  
);

INSERT INTO TA (TA_1)
   VALUES (1);
   
INSERT INTO TA (TA_1)
   VALUES (2);
   
INSERT INTO TA (TA_1)
   VALUES (3);
   
INSERT INTO TB(TB_1)
   VALUES (1);

INSERT INTO TB(TB_1)
   VALUES (2);
   
select * from TA natural join TB

code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

热风软妹 2025-02-01 10:46:33

不,没有简单的方法可以避免这种情况。
自然连接非常有风险,应尽可能防止。
自然连接需要列的身份名称和数据类型。这意味着每当仅在一张连接的表格中更改此操作时,它们就会失败,然后您必须找出在哪里以及什么。
正确的代码是无风险和正确工作的代码,而不是最短的代码。
也许您可以以一种非常不愉快的方式创建一个解决方法,以使其成为可能,但我建议只使用明确的加入。

No, there is no simple way to do this and you should avoid this.
Natural join is very risky and should be prevented whenever possible.
Natural joins require both the identic name and data type of the columns. This means they fail whenever this is changed in just one of the joined tables and then you have to find out where and what.
Correct code is code that works without risks and works correctly and not the shortest possible code.
Maybe you could create a workaround on a very unpleasant way to make it possible, but I recommend to just use explicit join.

埖埖迣鎅 2025-02-01 10:46:33

您可以做这样的事情:

WITH A AS (SELECT TA_1 FROM TA)
, B AS (SELECT TB_1 TA_1 FROM TB)
SELECT TA_1 FROM A NATURAL JOIN B;

关系数据库的一个基本特征是,加入是由查询定义的,而不是由架构定义。通常,您应该指定表中所需的列,而不是依靠选择 *。

You can do something like this:

WITH A AS (SELECT TA_1 FROM TA)
, B AS (SELECT TB_1 TA_1 FROM TB)
SELECT TA_1 FROM A NATURAL JOIN B;

A fundamental feature of relational databases is that joins are defined by queries, never by the schema. You should generally specify the columns required from your tables and not rely on SELECT *.

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