所有 SQL 查询都可以用关系代数、域和元组关系演算来表示吗

发布于 2024-12-09 05:02:46 字数 343 浏览 0 评论 0原文

我的查询包括拥有和计数或全部。这些在 RA/DRC/TRC 中如何表示?我是否必须进一步简化我的 SQL 查询?这是一个简化的示例:

empl(employee (primary key), city)
managers(employee (primary key), manager (foreign key of employee))

如果我要查找 X 市所有员工的经理(来自任何城市)的所有员工..我需要使用having/count。不知道在 RA/DRC/TRC 中这将如何完成。

我知道这样的查询的需要可能没有意义,但假设对于这个问题的目的来说这是合理的。

谢谢

My query includes a having and count or all in. How are these represented in RA/DRC/TRC? Would I have to simplify my SQL query even more? Here is a simplified example:

empl(employee (primary key), city)
managers(employee (primary key), manager (foreign key of employee))

If I were to find all the employees who are managers (from any city) of ALL the employees in city X.. I would need to use having/count. Not sure how this would be done in RA/DRC/TRC.

I know the need for such a query might not make sense but assume it is sensible for the purpose of this question.

Thanks

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

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

发布评论

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

评论(4

厌倦 2024-12-16 05:02:46

您的查询表述得有点含糊。确实是为了找到所有经理,他们是 X 市每个员工的经理?

正如 dportas 所指出的,这在 RA 中是完全可行的。

方法如下:

获取 X 市所有员工的集合。称之为 EMPX。

获取所有经理的集合。称之为 MGRS。

求两者的笛卡尔积。称之为 MGRS_EMPX。

从中减去表的实际值(适当地投影到所需的属性),该表表示哪些经理管理哪些员工。这种差异包含了真正存在的经理的所有组合,其中一名员工位于 X,但该经理并不管理该员工。

将这种差异投射到经理属性上。该关系告诉您存在哪些经理,使得 X 城市中存在一些不受该经理管理的员工。

从 MGRS 中减去该关系。显然,这种关系告诉您存在哪些经理,使得 X 城市不存在不受该经理管理的员工。

将存在量词的否定重写为通用量化将揭示这正是您想要的结果:NOT EXISTS(EMP:EMP 在 X 中并且 EMP 由 MGR 管理)=== FORALL EMP:NOT(EMP 在 X 中) AND EMP 由 MGR 管理) === FORALL EMP : (EMP 不在 X 中或 EMP 由 MGR 管理) === FORALL EMP :( 如果 EMP 在 X 中则EMP 由 MGR 管理)。

所有这些都是完美的代数运算。

(附带练习:看看如果 X 城市根本没有员工会发生什么。)

Your query was stated a bit ambiguous. It is indeed the intent to find all managers who are the manager for EACH AND EVERY employee that is in city X ?

As dportas indicated, that's perfectly doable in RA.

Here's how :

Get the collection of all the employees in city X. Call that EMPX.

Get the collection of all managers. Call that MGRS.

Make the cartesian product of the two. Call that MGRS_EMPX.

Subtract from that the actual value of the table (appropriately projected down to the needed attributes) that says which managers manage which employee. That difference holds all the combinations of managers that really exist, with an employee that is located in X, but where that manager does not manage that employee.

Project that difference down onto the manager attribute. That relation tells you which managers exist such that there exists some employee in city X that is NOT managed by that manager.

Subtract this relation from MGRS. Obviously, this relation tells you which managers exist such that there does NOT exist an employee located in city X that is NOT managed by that manager.

Rewriting this negation of an existential quantifier as a universal quantification will reveal that this is precisely the result that you want : NOT EXISTS (EMP : EMP is in X AND EMP managed by MGR) === FORALL EMP : NOT (EMP is in X AND EMP managed by MGR) === FORALL EMP : (EMP is not in X OR EMP is managed by MGR) === FORALL EMP : ( if EMP is in X then EMP is managed by MGR).

And all of these are perfectly fine algebra operations.

(Side exercise : see what happens if there are no employees located in city X at all.)

来世叙缘 2024-12-16 05:02:46

关系划分是您具体示例的答案 - 您不需要聚合。除法是代数的一部分。

您更普遍的问题是一个复杂的问题,因为确定任何可能的 SQL 结果何时与关系结果相同的问题。返回重复行的 SQL 查询是否等同于不返回重复行的关系表达式?带有 SQL 样式空值的 SQL 查询是否等同于任何关系表达式?如何仅使用 SQL 语法来表达没有属性的关系投影?

我认为唯一明智的答案是 SQL 模型和关系模型是两个完全不同且不兼容的事物。您可能不应该太费力地寻找他们之间的通信。

Relational Division is the answer to your specific example - you don't need an aggregate. Division is part of the algebra.

Your more general question is a complex one because of the problems of determining when any possible SQL result is the same as a relational one. Is a SQL query that returns duplicate rows equivalent to a relational expression that doesn't? Is a SQL query with SQL-style nulls equivalent to any relational expression? How do you express a relational projection over no attributes using only SQL syntax?

I think the only sensible answer is to say that the SQL model and relational one are two quite different and incompatible things. You probably shouldn't go looking too hard for correspondences between them.

怀念你的温柔 2024-12-16 05:02:46

也许您过于简单化了您的问题..但是为 X 市的员工寻找经理是一个简单的连接 - 没有计数或拥有。

编辑:

select  * 
from managers m,
( select employee from empl where city = 'XXXXX' ) e
where m.employee = e.employee

maybe you oversimplified your question.. but finding manager for employees in city X is a simple join - no Count or Having.

edit:

select  * 
from managers m,
( select employee from empl where city = 'XXXXX' ) e
where m.employee = e.employee
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文