将 SQL 查询转换为关系代数

发布于 2024-12-12 20:08:42 字数 525 浏览 3 评论 0原文

我需要一些帮助将 SQL 查询转换为关系代数。

这是 SQL 查询:

SELECT * FROM Customer, Appointment
WHERE Appointment.CustomerCode = Customer.CustomerCode
    AND Appointment.ServerCode IN
    (
        SELECT ServerCode FROM Appointment WHERE CustomerCode = '102'
    )
;

由于上面示例中的 IN 子查询,我陷入了困境。

谁能为我演示如何用关系代数表达这个 SQL 查询?

非常感谢。

编辑:这是我在关系代数中提出的解决方案。这是正确的吗?它会重现 SQL 查询吗?

代码 ← Π服务器代码(σCustomerCode='102'(预约))

C代码 ← Π客户代码(预约 ⋉ 代码)

结果 ← (客户 ⋉ C代码)

I need some help converting an SQL query into relational algebra.

Here is the SQL query:

SELECT * FROM Customer, Appointment
WHERE Appointment.CustomerCode = Customer.CustomerCode
    AND Appointment.ServerCode IN
    (
        SELECT ServerCode FROM Appointment WHERE CustomerCode = '102'
    )
;

I'm stuck because of the IN subquery in the above example.

Can anyone demonstrate for me how to express this SQL query in relational algebra?

Many thanks.

EDIT: Here is my proposed solution in relational algebra. Is this correct? Does it reproduce the SQL query?

Scodes ← ΠServerCode(σCustomerCode='102'(Appointment))

Ccodes ← ΠCustomerCode(Appointment ⋉ Scodes)

Result ← (Customer ⋉ Ccodes)

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

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

发布评论

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

评论(2

兔小萌 2024-12-19 20:08:43

您的 SQL 代码将导致 CustomerCode 出现重复的列,并且使用 SELECT [ALL] 可能会导致出现重复的行。因为结果不是关系,所以不能用关系代数来表达。

这些问题很容易在 SQL 中解决:

SELECT DISTINCT * 
  FROM Customer NATURAL JOIN Appointment
 WHERE Appointment.ServerCode IN
    (
        SELECT ServerCode FROM Appointment WHERE CustomerCode = '102'
    )
;

您没有指定您感兴趣的关系代数。Date 和 Darwen 提出了一个名为 A,指定名为 D,并设计了一种D语言,名为教程D< /a>.

教程 D 使用运算符 JOIN 进行自然连接,使用 WHERE 进行限制,使用 MATCHING 进行半连接,稍微复杂一点的是 SQL 中的比较:

CustomerCode = '102'

由于隐式强制转换,可以将 CustomerCode 值与 SQL 中的 CHAR 值进行比较。教程 D 更严格——类型安全,如果您愿意的话——要求您重载相等运算符,或者更实际地,为 CHAR 定义一个选择器运算符,它通常与类型具有相同的名称。

因此,上述(修改后的)SQL在教程D中可以写成:

( Customer JOIN Appointment ) 
   MATCHING ( ( Appointment WHERE CustomerCode = CustomerCode ( '102' ) ) { ServerCode } )

Your SQL code will result in duplicate columns for CustomerCode and the use of SELECT [ALL] is likely to result in duplicate rows. Because the result is not a relation, it cannot be expressed in relational algebra.

These problems are easily fixed in SQL:

SELECT DISTINCT * 
  FROM Customer NATURAL JOIN Appointment
 WHERE Appointment.ServerCode IN
    (
        SELECT ServerCode FROM Appointment WHERE CustomerCode = '102'
    )
;

You didn't specify which relational algebra you are intereted in. Date and Darwen proposed an algebra named A, specified an A language named D, and designed a D language named Tutorial D.

Tutorial D uses operators JOIN for natural join, WHERE for restriction and MATCHING for semijoin, The slight complication is the comparison in SQL:

CustomerCode = '102'

The comparison of a CustomerCode value to a CHAR value in SQL is possible because of implicit coercion. Tutorial D is stricter -- type safe, if you will -- requiring you to overload the equality operator or, more practically, define a selector operator for CHAR, which would typically have the same name as the type.

Therefore, the above (revised) SQL may be written in Tutorial D as:

( Customer JOIN Appointment ) 
   MATCHING ( ( Appointment WHERE CustomerCode = CustomerCode ( '102' ) ) { ServerCode } )
毁我热情 2024-12-19 20:08:43

“如何用 RA 的标准形式表示我的查询?”

这与其说是“代数类型”的问题,不如说是“符号类型”的问题。

使用希腊符号的表示法通常使用 sigma、附加到 sigma 字符的下标中的限制条件,然后是限制的主题(受限制条件约束的关系表达式)。

日期避免使用这种符号,因为使用这种符号排版和/或创建文本通常比仅使用西方字母要困难得多(我的一位数学老师曾经告诉我们,数学教科书包含的错误最多)。

σ() 因此表示与(日期语法)“WHERE”完全相同的代数表达式。

类似地,对于希腊符号,投影通常使用字母 Pi 来表示,Pi 后面附加下标中的保留属性列表,后面是作为投影主题的表达式。

Π <属性列表> () 因此表示与(日期语法)“{}”完全相同的代数表达式。

连接系列运算符通常用“希腊”符号表示,使用 Unicode BOWTIE 字符(的变体),或者由小写字母“x”组成的字符,周围有一个完整的圆圈(通常用于表示完整的笛卡尔积,叉积,...无论你的代数课程碰巧如何命名它)。

有些课程使用希腊字母 Rho 提供用于重命名的“希腊符号”符号。下标中附加的是重命名列表,其形式为a1->b1,a2->b2,...,其后附加的是要进行重命名的关系表达式。同样,日期具有非希腊符号等效语法:; RENAME a1 AS b1, a2 AS b2 , ...

重要的是要看到这些差异仅仅是语法符号的差异,而不是“不同的代数”。

编辑

人们可以想象,希腊符号表示法是将关系代数编程到 APL 引擎中的方法,日期语法是将关系代数编程到类似 cobol 或 PL/1 的引擎中的方法(实际上存在这样一个称为 Rel 的引擎,以及将关系代数编程为类似 OO 的引擎的方法,可能类似于关系.NaturalJoin(otherRelation).Matching(yetOtherRelation.Restrict(condition).project(attributesList))。

"How do I represent my query in this standard form of RA?"

It's not so much a question of "type of algebra" as it is of "type of notation".

Notation using greek symbols typically uses sigma, the restrict condition in subscript appended to the sigma character, and then the subject of the restriction (the relational expression that is subjected to the restrict condition).

Date avoid that notation, because typesetting and/or creating text using such notations is usually a lot harder than it is using just the western alphabet (a math teacher of mine once told us that math textbooks contain the most errors of all).

σ <cond> (<rel exp>) thus denotes the very same algebra expression as (Date's syntax) "<rel exp> WHERE <cond>".

Similarly, with greek symbols, projection is typically denoted using the letter Pi, with the list of retained attributes in subscript appended to the Pi, and the expression that is the subject of the projection following that.

Π <attr list> (<rel exp>) thus denotes the very same algebra expression as (Date's syntax) "<rel exp> { <attr list> }".

The join family of operators is usually denoted, in "greek" symbols, using (variations of) the Unicode BOWTIE character, or that character consisting of a lowercase letter 'x' surrounded by a full circle (usually used to denote full cartesian product, cross-product, ... whatever your algebra course happens to name it).

Some courses provide a "greek-symbol" notation for rename, using the greek letter Rho. Appended in subscript is the rename list, in the form a1->b1,a2->b2,... Appended after that comes the relational expression that is subjected to the rename. Likewise, Date has a non-greek-symbol equivalent syntax : <rel exp> RENAME a1 AS b1, a2 AS b2 , ...

The important thing is to see that these differences are merely differences in syntactical notation, not "different algebrae".

EDIT

One could imagine that the greek symbols notation would be the way to program relational algebra into an APL engine, Date's syntax would be the way to program relational algebra into a cobol-like or PL/1-like engine (there effectively exists such an engine called Rel), and the way to program relational algebra into an OO-like engine, could look something like relation.NaturalJoin(otherRelation).Matching(yetOtherRelation.Restrict(condition).project(attributesList)).

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