gpt4 book ai didi

sql - 自然连接的维恩图

转载 作者:搜寻专家 更新时间:2023-10-30 22:05:45 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

我们可以让一个区域成为一个集合,其元素是关联表的列名。那么左右circle元素就是左右表的列名,overlap元素就是公共(public)的列名(每个输入表中必须出现一次),合并的元素就是结果的列名。但是 SQL 表可以有重复的列名,并且列是有序的;所以这不会给出结果列列表。

我们可以让一个区域成为一个集合,其元素是公共(public)列下的不同子行值。那么左右circle元素就是左右表子行值,overlap元素就是结果子行值。但这并没有给出子行值在表中出现的次数。特别是,当所有列都通用时,子行值就是行值,这不会给出行值在结果中出现的次数。

图表和配对都没有给出输出行的一般情况。

来自 my answerCROSS JOIN vs INNER JOIN in SQL :

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.

来自 one of my comments在一个答案中有内部连接的维恩图:

Figure 1 is a common terrible attempt to explain JOIN. Its [legend] 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.

来自 one of my commentsWhat 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.

another :

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.)

因此维恩图与某些情况相关在这些情况下可以合理地认为表格包含行值元素集但一般而言,SQL 表不包含行值元素的集合,而维恩图表示集合。

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

来 self 对 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 answerWhat 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当他们真的是Euler diagrams .

关于sql - 自然连接的维恩图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55639641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com