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

标签 relational-algebra relational relational-division

教授给了我以下任务: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 ⋈ Π city (σ company_name=”Small Bank Cooperation” (company)))

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

最佳答案

你的不一样。

My answer here says how to query relationally.它使用关系代数的一个版本,其中标题是属性名称的集合。 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

您返回的行的列数多于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

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

当您看到与其他一些行的“每个”或“所有”匹配的行的查询时,您可以预期查询的该部分涉及 或者一些相关的习语。确切的代数取决于(经常表达得不好/含糊不清)要求的意图。例如,当不存在此类城市时,“位于其中的每个城市的公司”是否应该是没有公司(部门)或所有公司(相关惯用语)。 (你的作业的正常数学解释是后者。)例如,他们是否想要在所有这些城市或至少在所有这些城市都有公司。

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

Database Relational Algebra: How to find actors who have played in ALL movies produced by “Universal Studios”?
How to understand u=r÷s, the division operator, in relational algebra?
How to find all pizzerias that serve every pizza eaten by people over 30?

关于relational-algebra - 这种自然连接操作是否正确使用? (关系代数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64441677/

相关文章:

javascript - Couchdb 使用键连接两个文档

sql - 关系代数和混淆一个复杂的例子

sql - 努力思考这个数据库查询

php - 在项目建模时使用数据库

sql - 在 SQL 连接的情况下如何将 SQL 转换为关系代数?

python - python中的关系数据结构

c# - 使用 NHibernate 实现全局化

sql - 棘手的 sql 查询 - 寻找替代供应商(关系部门)

sql - PostgreSQL - WHERE 条件指的是 2 个不同的记录并且应该是 TRUE

mysql - 计算类别 A 和 B 中的项目 (MySQL)