如何最好地解释用户应该加入哪些领域?

发布于 2024-11-18 08:42:32 字数 179 浏览 3 评论 0原文

我需要向某人解释他们如何确定应该加入多个表/视图中的哪些字段。有什么建议吗?我知道该怎么做,但很难解释它。

他们遇到的问题之一是,他们将从两个相同的表(邮政编码)中获取两个字段并连接这些字段,而实际上它们应该连接 ID 列。当他们选择错误的列加入时,他们收到的记录就会增加。

我应该在PK和FK的某个地方工作吗?

I need to explain to somebody how they can determine what fields from multiple tables/views they should join on. Any suggestions? I know how to do it but am having difficulty trying to explain it.

One of the issues they have is they will take two fields from two tables that are the same (zip code) and join on those, when in reality they should be joining on ID columns. When they choose the wrong column to join on it increases records they receive in return.

Should I work in PK and FK somewhere?

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

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

发布评论

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

评论(7

甜味超标? 2024-11-25 08:42:32

虽然将 PK 加入 FK 确实是典型的做法,但任何有关仅围绕 PK 和 FK 的 JOIN 子句的对话都相当有限。

例如,我在一个最近的 SQL 我给出的答案

FROM
  YourTable firstNames 
   LEFT JOIN YourTable  lastNames 
       ON firstnames.Name = lastNames.Name
         AND lastNames.NameType  =2
          and firstnames.FrequencyPercent < lastNames.FrequencyPercent

表的每一侧引用的表都是同一个表(自连接),它包含三个条件,其中一个是不等式。此外,这里永远不会有 FK,因为它希望加入一个字段,这是设计使然,而不是候选键。

此外,您甚至不必将一张桌子连接到另一张桌子。您可以将内联查询相互连接,这当然不可能有键。

因此,为了正确理解 JOIN,您只需要了解它组合了来自两个关系(表、视图、内联查询)的记录,其中某些条件评估为 true。这意味着您需要了解布尔逻辑和数据库以及数据库中的数据。

如果您的用户在特定 JOIN 方面遇到问题,请要求他们从一个表和另一个表中选择一些行,然后询问他们您希望在什么条件下合并这些行。

While it is indeed typical to join a PK to an FK any conversation about JOIN clauses that only revolve around PK's and FK's is fairly limited

For example I had this FROM clause in a recent SQL answer I gave

FROM
  YourTable firstNames 
   LEFT JOIN YourTable  lastNames 
       ON firstnames.Name = lastNames.Name
         AND lastNames.NameType  =2
          and firstnames.FrequencyPercent < lastNames.FrequencyPercent

The table referenced on each side of the table is the same table (a self join) and it includes three condidtions one of which is an inequality. Furthermore there would never be an FK here because its looking to join on a field, that is by design, not a Candidate Key.

Also you don't have even have to join one table to another. You can join inline queries to each other which of course can't possibly have a Key.

So in order to properly understand JOIN you just need to understand that it combines the records from two relations (tables, views, inline queries) where some conditions evaluate to true. This means you need to understand boolean logic and the database and the data in the database.

If your user is having a problem with a specific JOIN ask them to SELECT some rows from one table and also the other and then ask them under what conditions would you want to combine the rows.

甜中书 2024-11-25 08:42:32

您不需要谈论表的主键,但您应该指出它并解释它唯一标识给定的行,并且您必须使用它连接到相关表,否则可能会得到重复的结果。

给他们提供使用它加入和不使用它加入的例子。

显示他们使用的所有表及其键关系的 ER 图将有助于确保他们始终使用正确的键。

You don't need to talk in terms of a primary key of a table but you should point to it and explain that it uniquely identifies a given row and that you must join to related tables using it or you could get duplicated results.

Give them examples of joining with it and joining without it.

An ER diagram showing all of the tables they use and their key relationships would help ensure that they always use the correct keys.

站稳脚跟 2024-11-25 08:42:32

在我看来,您和您试图帮助的人都不了解这个特定数据库的构建方式,甚至可能根本不了解基本的数据库基础知识,例如 PK 和 FK。大多数情况下,一个表中的 PK 会连接到另一个表的 FK。

假设数据库具有适当的 PK 和 FK,那么生成 ER 图可能会有很大帮助。这将使连接概念更容易掌握。

您可以采取的另一种方法是找到真正了解这些事情的人并创建一些观点供此人使用。这样他就不需要了解如何将表连接在一起。

It sounds to me like neither you, nor the person you are trying to help understands how this particular database is constructed and perhaps don't really even understand basic database fundamentals, like PK's and FK's. Most often a PK from one table is joined to a FK to another table.

Assuming the database has the proper PK's and FK's in place, it would probably help a great deal to generate an ER diagram. That would make the joining concept much easier to grasp.

Another approach you could take is to find someone who does understand these things and create some views for this person to use. This way he doesn't need to understand how to join the tables together.

弥繁 2024-11-25 08:42:32

用户通常不应该进行连接。用户应该有一个界面,让他们能够以所需的方式获取所需的数据。如果您没有开发人员资源来做到这一点,那么您将陷入必须向用户传授技术细节的问题。您还需要非常小心用户可能造成的损害。他们有数据更新权吗?我希望他们不会意外地执行没有 WHERE 子句的 DELETE FROM Table 操作。即使您限制他们的权限,编写不当的查询也可能会破坏数据库服务器或阻止资源,从而给其他用户带来问题(并为您带来更多工作)。

如果您别无选择,那么我认为您当然需要教他们主键和外键,即使您不这样称呼它们。指出你的表上的 id(或者你的 PK 是什么)标识一行。然后解释一下id如何出现在其他表中以显示关系。例如,“看,在地址表中,我们有一个 person_id,它告诉我们该地址属于谁。”

之后,预计您将花费大量时间与该用户相处,因为他们会犯错误或提出他们想从数据库中获取但不知道如何获取的其他内容。

A user shouldn't typically be doing joins. A user should have an interface that lets them get the data that they need in the way that they need it. If you don't have the developer resources to do that then you're going to be stuck with this problem of having to teach a user technical details. You also need to be very careful about what kind of damage the user can do. Do they have update rights on the data? I hope they don't accidentally do a DELETE FROM Table with no WHERE clause. Even if you restrict their permissions, a poorly written query can crush the database server or block resources causing problems for other users (and more work for you).

If you have no choice, then I think that you need to certainly teach them about primary and foreign keys, even if you don't call them that. Point out that the id on your table (or whatever your PK is) identifies a row. Then explain how the id appears in other tables to show the relationship. For example, "See, in the address table we have a person_id which tells us who that address belongs to."

After that, expect to spend a large portion of your time with that user as they make mistakes or come up with other things that they want to get from the database, but which they can't figure out how to get.

つ低調成傷 2024-11-25 08:42:32

从理论上讲,理想情况下,您应该在所有表上定义主键,并使用主键将表连接到另一个表中的匹配字段或字段(外键)。
即使您没有定义或者它们没有定义为主键,您也需要确保这些字段唯一标识表中的记录,并且应该对它们进行正确的索引。
例如,假设“person”表有一个 SSN 和一个驾驶执照字段。 SSN 可以被考虑并标记为“主键”,但如果您将该表加入到“驾驶员”表中,该表可能没有 SSN,但确实有驾驶执照 #,您可以通过驾驶执照字段加入它们(即使它没有标记为主键),但您需要确保该字段在两个表中都正确索引。

From theory, and ideally, you should define primary keys on all tables, and join tables using a primary key to the matching field or fields (foreign key) in the other table.
Even if you don't define or if they're not defined as primary keys, you need to make sure the fields uniquely identify the records in the table, and that they should be properly indexed.
For example, let's say the 'person' table has a SSN and a driver's license field. The SSN could be considered and flagged as the 'primary key', but if you join that table to a 'drivers' table which might not have the SSN, but does have the driver's license #, you could join them by the driver's license field (even if it's not flagged as primary key), but you need to make sure that the field is properly indexed in both tables.

我的痛♀有谁懂 2024-11-25 08:42:32

...向某人解释他们如何确定应该加入多个表/视图中的哪些字段。

简而言之,查找表/视图之间具有匹配值的列。最好是完全匹配,但可能需要一些按摩。

外键约束的存在将有助于了解什么与什么匹配,但约束可能不会直接针对要连接的表/视图。

主键的存在并不意味着它是查询所必需的标准,因此我会忽略这个细节(取决于受众)。

我建议通过从所需的列开始并从那里返回来攻击所需的结果集。如果结果集中有多个表的列,则首先关注其列应返回不同结果的表,然后逐渐添加联接,检查每次 JOIN 添加之间的结果集以确认结果仍然相同。否则,需要检查 JOIN 或 JOIN 是否确实必要,而不是 INEXISTS

...explain to somebody how they can determine what fields from multiple tables/views they should join on.

Simply put, look for the columns with values that match between the tables/views. Preferably, match exactly but some massaging might be necessary.

The existence of foreign key constraints would help to know what matches to what, but the constraint might not be directly to the table/view that is to be joined.

The existence of a primary key doesn't mean it is the criteria that is necessary for the query, so I would overlook this detail (depending on the audience).

I would recommend attacking the desired result set by starting with the columns desired, and working back from there. If there's more than one table's columns in the result set, focus on the table whose columns should be returning distinct results first and then gradually add joins, checking the result set between each JOIN addition to confirm the results are still the same. Otherwise, need to review the JOIN or if a JOIN is actually necessary vs IN or EXISTS.

泪意 2024-11-25 08:42:32

我刚开始时就这样做了,它来自于将联接视为只是将表连接在一起,所以我在所有可能的点上进行了链接。

一旦您将联接视为组合和过滤数据的一种方式,理解它们就会变得更容易。

将您的请求写成一个句子也很有帮助,“我想查看表 A 与表 B 交互的所有时间”。然后仅使用 ID 构建一个查询,请注意,如果您想知道“表 A 与表 B 的邮政编码始终相同”,那么您将通过邮政编码加入。

I did this when I first started out, it comes from thinking of joins as just linking tables together, so I linked at all possible points.

Once you think of joins as a way to combine AND filter the data it becomes easier to understand them.

Writing out your request as a sentence is helpful too, "I want to see all the times Table A interacted with Table B". Then build a query from that using only the ID, noting that if you wanted to know "All the times Table A was in the same zip code as Table B" then you would join by zip code.

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