这种自然连接操作是否正确使用? (关系代数)

Is this natural join operation used correctly? (Relational Algebra)

教授给我的任务如下:R-E Modell

  1. Assume the companies may be located in several cities. Find all companies located in every city in which “Small Bank Corporation” is located.

现在教授的解决方案如下:

s ← Π city (σ company_name=’Small Bank Corporation’ (company))
temp1 ← Π comp_id, company_name (company)
temp2 ← Π comp_id, company_name ((temp1 × s) − company)
result ← Π company_name (temp1 − temp2)

我自己找到了一个完全不同的解决方案,它有一个自然的连接操作,看起来简单得多:

我尝试做的是使用自然联合操作,它定义为如下关系 r 和 s 在它们的共同属性上连接。因此,我尝试通过使用 company_name“Small Bank Cooperation”对所有公司的选择进行投影来获取所有城市名称。之后我加入了 table 和公司 table 的城市名称,这样我就得到了所有包含城市名称的公司条目。

company ⋈ Π city (σ company_name=”Small Bank Cooperation” (company)))

我现在的问题是我的解决方案是否也有效,因为它看起来有点微不足道?

你的不一样。

My answer here says how to query relationally. It uses a version of the relational algebra where headings are sets of attribute names. My answer here summarizes it:

Every query expression has an associated (characteristic) predicate--statement template parameterized by attributes. The tuples that make the predicate into a true proposition--statement--are in the relation.

We are given the predicates for expressions that are relation names.

Let query expression E have predicate e. Then:

  • R ⨝ S has predicate r and s
  • R ∪ S has predicate r or s
  • R - S has predicate r and not s
  • σ p (R) has predicate r and p
  • π A (R) has predicate exists non-A attributes of R [r]

When we want the tuples satisfying a certain predicate we find a way to express that predicate in terms of relation operator transformations of given relation predicates. The corresponding query returns/calculates the tuples.

您的解决方案

company ⋈ Π city (σ company_name=”Small Bank Corporation” (company)))

所在的行
    company company_id named company_name is in city
AND FOR SOME company_id & company_name [
            company company_id named company_name is in city
        AND company_name=”Small Bank Corporation”]

    company company_id named company_name is in city
AND FOR SOME company_id [
        company company_id named ”Small Bank Corporation” is in city]

    company company_id named company_name is in city
AND some company named ”Small Bank Corporation” is in city

您正在 returning 行的列数多于 company_name。但是你的公司不是要求的公司。

将您的行投影到 company_name 得到行

    some company named company_name is in some city
AND some company named ”Small Bank Corporation” is in that city

After that I joined the table with the city names with the company table, so that I get all company entrys which have the city names in it.

不清楚你得到了什么。但是,您所在行中的公司至少位于 SBC 城市之一。该请求适用于所有 SBC 城市:

companies located in every city in which “Small Bank Corporation” is located

我提供的链接告诉您如何编写查询,以及如何在查询结果规范和关系代数表达式之间转换return结果。

当您看到查询匹配某些其他行的“每个”或“所有”行时,您可以预期查询的那部分涉及 或一些相关的习语。确切的代数取决于 - 经常 poorly/ambiguously 表达的 - 要求的意图。例如,“companys located in every city in which”是否应该是没有公司(部门)或所有公司(相关成语),当没有这样的城市时。 (你的作业的正常数学解释是后者。)例如,他们是否希望在所有这些城市或至少在所有这些城市都有公司。

(这有助于避免在“find”和“return”之后使用“all”和“every”,无论如何都是多余的。)



How to find all pizzerias that serve every pizza eaten by people over 30?