自然连接的维恩图

Venn Diagram for Natural Join

我一直在努力理解 sql 完全连接的概念,维恩图在这方面帮助了我很多。我发现它们适用于所有类型的连接,但不适用于自然连接。

自然连接的维恩图是什么样的?

维恩图对于理解自然连接或内连接不是很有帮助。大多数与 Stack Overflow 上的连接相关的维恩图都是重复的毫无价值的虚假陈述——即使在维恩图可能有用的情况下也是如此。

以下是 SQL 自然连接的维恩图的一些有效用法:

如果您忽略列顺序,我们可以将一个区域作为一个集合,其元素是关联的 table 的列名。那么左右圆圈的元素就是左右tables的列名&合并后的元素就是结果的列名。

如果具有相同名称的输入 table 列具有相同的类型,那么我们可以将一个区域作为集合,其元素是出现在 table 中某处的公共列的子行值.那么左右圆圈的元素就是左右tables这样的子行值,交集元素就是结果的这样子行值。

但是图和对都没有告诉我们输出行是什么。

来自我在 CROSS JOIN vs INNER JOIN in SQL Server 2008 的回答:

Re Venn diagrams A Venn diagram with two intersecting circles can illustrate the difference between output rows for INNER, LEFT, RIGHT & FULL JOINs for the same input. And when the ON is unconditionally TRUE, the INNER JOIN result is the same as CROSS JOIN. Also it can illustrate the input & output rows for INTERSECT, UNION & EXCEPT. And when both inputs have the same columns, the INTERSECT result is the same as for standard SQL NATURAL JOIN, and the EXCEPT result is the same as for certain idioms involving LEFT & RIGHT JOIN. But it does not illustrate how (INNER) JOIN works in general. That just seems plausible at first glance. It can identify parts of input and/or output for special cases of ON, PKs (primary keys), FKs (foreign keys) and/or SELECT. All you have to do to see this is to identify what exactly are the elements of the sets represented by the circles. (Which muddled presentations never make clear.) (Remember that in general for joins output rows have different headings from input rows.)

我强调重复:

But it does not illustrate how (INNER) JOIN works in general.

All you have to do to see this is to identify what exactly are the elements of the sets represented by the circles.

my comments (using "key" in the sense of "legend") on an answer 重新获取内部连接的维恩图 "Figure 1":

Figure 1 is a common terrible attempt to explain JOIN. Its key is also complex: It's only for tables as sets & only equijoin & only one [column]; it also represents the input differently than the output. Write it for JOIN in general.

来自我对 What is the difference between “INNER JOIN” and “OUTER JOIN”? 的评论:

Venn diagrams show elements in sets. Just try to identify exactly what the sets are and what the elements are in these diagrams. The sets aren't the tables and the elements aren't their rows. Also any two tables can be joined, so PKs & FKs are irrelvant. All bogus. You are doing just what thousands of others have done--got a vague impression you (wrongly) assume makes sense.

Of the answers & comments & their references below only one actually explains how Venn diagrams represent the operators: The circle intersection area represents the set of rows in A JOIN B. The area unique to each circle represents the set of rows you get by taking its table's rows that don't participate in A JOIN B and adding the columns unique to the other table all set to NULL. (And most give a vague bogus correspondence of the circles to A and B.)

因此,维恩图与某些情况相关 ,其中 table 可以合理地视为包含行值元素集 但通常 SQL tables 不包含行值元素的集合,而维恩图表示集合。

通过维恩图重新说明内部联接与外部联接:

来自my comment on LEFT JOIN vs. LEFT OUTER JOIN in SQL Server

Re Venn diagrams: If no nulls or duplicate rows are input, so we can take a table to be a set of row-valued values & use normal math =, then the Venn diagrams are OK--taking circles to hold left & right join output tables/sets. But if nulls or duplicate rows are input then it is so difficult to explain just what the circles are sets of & how those sets relate to input & output tables/bags that Venn diagrams are not helpful.

来自 my comment on my answer at What is the difference between “INNER JOIN” and “OUTER JOIN”?

I must admit that, despite my quick phrasing in comments, because SQL involves bags & nulls and SQL culture doesn't have common terminology to name & distinguish between relevant notions, it is non-trivial even to explain clearly how elements of a Venn diagram are 1:1 with output "rows", let alone input "rows". Or what inner or outer joins do, let alone their difference. "value" may or may not include NULL, "row" may be a list of values vs a slot in a table value or variable & "=" may be SQL "=" vs equality.

PS 通常图表被称为 Venn diagrams when they are really Euler diagrams.