这些操作对这些表的结果是什么?

时间:2017-10-03 19:17:26

标签: database join relation relational-algebra

Enroll (table):

cid        sid    grade    gradepoint
CS425      001    A        4.0
CS595      001    B        3.0
CS595      002    A        4.0
EE401      001    A        4.0
EE401      002    B        3.0
EE401      004    A        4.0
PHYS571    002    C        2.0
PHYS571    004    A        4.0

使用关系代数,操作是(借口缺少希腊字母,我不知道如何在SO上添加它们):

select_(Enroll.gradepoint<x.gradepoint)^Enroll.cid=x.cid (Enroll x rho_x(Enroll))

基本上,它是说自己做一个Enroll表的笛卡尔积(将第二个Enroll重命名为&#34; x&#34;),然后只选择cid匹配的行(当然)和等级点低于自身。但这毫无意义。这不会导致空集吗?

1 个答案:

答案 0 :(得分:1)

假设运算符rename n (r)返回类似r的表值,但使用新的属性名称,例如前缀为n.的旧属性名称。然后我们有:

/* rows where
    (cid, sid, grade, gradepoint) in Enroll
and (x.cid, x.sid, x.grade, x.gradepoint) in Enroll
and gradepoint < x.gradepoint and cid = x.cid
*/
restrict gradepoint < x.gradepoint and cid = x.cid (
    Enroll times rename x (Enroll)
    )

(哪些是(cid, sid, grade, gradepoint, x.cid, x.sid, x.grade, x.gradepoint)形式的行。)

不幸的是,评论没有说明业务情况。所以桌子也没有。但是在我们的案例中假设(cid, sid, grade, gradepoint) in Enroll in course cid student sid got grade grade & grade point average 的。 (这是业务术语中表的(特征)谓词。)然后,通过替换gradepoint,我们得到该查询也是:

in

表值中的每一行(常量/变量或查询结果)都会将语句插入表的谓词中。 (并且每个缺席行都声明语句的否定/ /* rows where in course cid student sid got grade grade & grade point average gradepoint and in course x.cid student x.sid got grade x.grade & grade point average x.gradepoint and gradepoint < x.gradepoint and cid = x.cid */ 将其插入到谓词中。)设计者给出常量/变量谓词,查询结果谓词来自那些&amp;关系运算符。 (not&amp; restrict s(包括join&amp; times)介绍intersectand介绍project,{{ 1}}介绍there existsunion介绍or等等。)

在这里,声明涉及一个课程和两个成绩点,每个成绩点属于不同的学生。所以没有“等级低于自身的地方”。

Forming a relational algebra query from an English description
Is there any rule of thumb to construct SQL query from a human-readable description?

(所谓的“代数运算符”就像你提到“重命名”一个值实际上是一个编程语言非终端,用于赋值给变量。与代数值和运算符正交。这种混乱源于思维不清晰,这是SQL护教学的典型特征。)