关系代数中允许乘法吗? [英] Is multiplication allowed in relational algebra?

查看:133
本文介绍了关系代数中允许乘法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关系

R
-------
cid     sid        gradepoint        credits

CS425   001        4.0               3

我需要计算GPA。还有更多的行,但是我相信,如果我得到这个答案,我应该对其余的一切都满意。我需要做积分*学分。我该如何用关系代数表达呢?

I need to calculate the GPA. There are more rows, but I believe if I just get this answered I should be ok with the rest. I need to do gradepoint * credits. How do I express this with a relational algebra expression?

我最好的猜测是:

,但是我不确定是否可以将属性与 other

, but I'm not sure if I can multiply attributes with anything other than a constant.

推荐答案

请注意,如果允许使用像您这样的表达式,则投影就是乘法。关系值&而不是其输入。属性名称,其输入是一个关系值和某种类型的表达式,其中包括以属性类型的值作为输入的运算符的名称。您的投影正在解析和相乘。因此,与仅接受属性名称的操作符相比,它是一个不同的操作符

Notice that if expressions like you are using are allowed then it is projection that is doing the multiplying. Instead of its inputs being a relation value & attribute names, its inputs are a relation value and expressions of some sort that include names of operators whose inputs are values of attribute types. Your projection is parsing and multiplying. So it is a different operator than one that only accepts attribute names.

采用属性表达式的投影将乞求其实现的问题。仅在关系值和属性名称上进行投影的代数。这在学术环境中很重要,因为一个问题可能希望您真正想出该怎么做,或者因为问题的难度取决于可用的运算符。因此,找出您应该使用的代数。

The projection that takes attribute expressions begs the question of its implementation given an algebra with projection only on a relation value and attribute names. This is important in an academic setting because a question may be wanting you to actually figure out how to do that, or because the difficulty of a question is dependent on the operators available. So find out what algebra you are supposed to use.

当我们只有基本的关系运算符使用属性名称和关系值时,我们可以为属性值引入一个运算符。每个这样的运算符都可以与一个关系值相关联,该关系值具有每个操作数的属性和结果的属性。该关系包含元组,其中结果值等于在操作数值上调用的运算符的结果。 (结果在功能上取决于操作数。)

We can introduce an operator on attribute values when we only have basic relation operators taking attribute names and relation values. Each such operator can be associated with a relation value that has an attribute for each operand and an attribute for the result. The relation holds the tuples where the result value is equal to the the result of the operator called on the operand values. (The result is functionally dependent on the operands.)

因此,假设我们具有下表值 Times 个元组其中左*右=结果

So suppose we have the following table value Times holding tuples where left * right = result:

left  right  result
-------------------
 0     0      0
 1     0      0
 ...
 0     1      0
 1     1      1
 2     1      2
 ...

如果您的计算出的属性是结果,那么您想要

If your calculated attribute is result then you want

/* tuples where for some credits & gradepoint,
        course cid's student sid earned grade gradepoint and credits     credits
    and credits * gradepoint = result
*/ 
project cid, sid, result (
    R natural join (rename left\credits right\gradepoint (Times))
    )

关系代数-重新编码列值

PS re代数与语言:什么是对您使用的关系代数的引用?有许多。他们甚至对关系有不同的看法。某些所谓的代数实际上是语言,因为表达式不仅代表表示在值上调用运算符的结果。尽管代数可能具有表示表达式和/或包含其自身名称的关系值的操作数值。

PS re algebra vs language: What is a reference to the "relational algebra" you are using? There are many. They even have different notions of what a "relation" is. Some so-called "algebras" are really languages because the expressions don't only represent the results of operators being called on values. Although it is possible for an algebra to have operand values that represent expressions and/or relation values that contain names for themselves.

PS重新分离关注点:您尚未表示乘法结果的属性名称是什么。如果您期望它是信用*成绩点,那么您期望投影将表达式值的输入映射到属性名称。除非您希望将学分*成绩点识别为具有两个属性名称&的表达式。一个运算符的名称放在一个地方,而只是另一个的属性名称。语言设计中有针对这些问题的解决方案,例如专用于属性名称的SQL可选引号。但是也许您可以理解为什么简单的事情,例如仅对属性名称&进行运算的代数。具有唯一,无序属性名称的关系值有助于我们理解独立的块。

PS re separation of concerns: You haven't said what the attribute name of the multiplication result is. If you're expecting it to be credit * gradepoint then you're also expecting projection to map expression-valued inputs to attribute names. Except you are expecting credit * gradepoint to be recognized as an expression with two attribute names & and an operator name in one place but to be just one attribute name in another. There are solutions for these things in language design, eg in SQL optional quotes specialized for attribute names. But maybe you can see why simple things like an algebra operating on just attribute names & relation values with unique, unordered attribute names helps us understand in terms of self-contained chunks.

这篇关于关系代数中允许乘法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆