关系代数 - 重新编码列值
Relational algebra - recode column values
假设我有一个 table 'animals',其行代表不同的动物,并且有一列 species 可能具有 'cat'、'dog'、[=19 等值=], 'cow' 等。假设我只对动物是否是狗感兴趣。在 sql 中(至少在 MySQL 中)我可以像 isDog from animals[=15] 那样查询 select (species='dog') =] 到 return 狗为 1,否则为 0。我如何在 RA 中表达这个?它不是 selecting 因为我们没有限制行数。即使我的表达式 (species='dog') 本身不是属性,我也可以使用项目运算符吗?或者我该如何处理?
编辑:
我想通过在不存在但基于语句真值的列上使用项目运算符来实现结果。例如,table 只包含一列的动物行 'species' 有行:猫、狗、马、牛。我需要可以重命名为 'isDog' 的布尔值,这将导致值 0,1,0,0(1=true,0=false)。我通过 selecting (species='dog') 作为 isDog 在 MySQL 中获得此信息,我想知道将项目操作员与 (species='dog') 选择这样一个动态创建的列,还是有其他方法来处理这个问题?
TL;DR 要将特定值引入关系代数表达式,您必须有一种编写 table 文字的方法。通常必要的运算符没有明确说明,但另一方面,代数练习经常使用 一些 种符号来表示示例值。
对于您的情况,使用最简单的附加关系(使用类似于 SQL VALUES 的临时 table 文字表示法):
(restrict SPECIES=dog Animals) natural join TABLE{ISDOG}{<1>}
union (restrict SPECIES<>dog Animals) natural join TABLE{ISDOG}{<0>}
如果您想参考具有用于一般计算的运算符的关系代数,请参阅作者 Chris Date 的 EXTEND 运算符和他的经典教科书数据库系统简介,第 8 版。
(更一般地说:要输出一个 table,其中包含新列的行,其值是输入行中值的任意函数,我们必须能够访问对应于运算符的 tables。要么我们使用 table 文字或者我们假设一个布尔函数并将其名称用作 table 名称。)
但事实证明关系模型的设计是为了:
每个代数运算符对应一个特定的逻辑运算符。
NATURAL JOIN
& AND
RESTRICT
theta
& AND
theta
UNION
& OR
MINUS
& AND NOT
PROJECT
all but
C
& EXISTS C
etc
每一个嵌套的代数表达式对应一个特定的嵌套逻辑表达式。
Animals
&
"animal named NAME is AGE years old ... and is of species SPECIES"
restrict SPECIES=dog Animals
&
"animal named NAME is AGE years old ... and is of species SPECIES" AND SPECIES=dog
一个命题是一个陈述。 predicate 是一个语句模板。如果每个基数 table 包含由其列参数化的谓词构成真命题的行,则查询包含由其列参数化的相应谓词构成真命题的行。
-- table of rows where
animal named NAME is AGE years old ... and is of species SPECIES
Animals
-- table of rows where
animal named NAME is AGE years old ... and is of species SPECIES
AND if SPECIES=dog then ISDOG=1 ELSE ISDOG=0
-- ie rows where
animal named NAME is AGE years old ... and is of species SPECIES
AND (SPECIES=dog AND ISDOG=1 OR SPECIES<>DOG AND ISDOG=0)
-- ie rows where
animal named NAME is AGE years old ... and is of species SPECIES
AND SPECIES=dog AND ISDOG=1
OR animal named NAME is AGE years old ... and is of species SPECIES
AND SPECIES<>dog AND ISDOG=0
(restrict SPECIES=dog Animals) natural join TABLE{ISDOG}{<1>}
union (restrict SPECIES<>dog Animals) natural join TABLE{ISDOG}{<0>}
所以你可以只用逻辑,工程(包括软件)、科学(包括计算机)和数学中精确的语言来描述你的结果tables。
-- table of rows where
animal named NAME is AGE years old ... and is of species SPECIES
AND ISDOG=(if SPECIES=dog then 1 else 0)
因此您可以在规范中使用 table 表达式 and/or 逻辑表达式,以(子)表达式逐(子)表达式为基础,以更清晰的为准.
Animals
natural join
table of rows where if SPECIES=dog THEN ISDOG=1 ELSE ISDOG=0
(对应于该 IF 表达式的 table 每个字符串都有一行,而 'dog' 行是唯一带有 1 的行。)
(Nb SQL ON & WHERE 左边有这种形式的 a table,右边有 predicate with functions。)
- 代数表达式计算满足其对应逻辑表达式的行。
你可能不太清楚什么
等价关系表达式对应于什么等价逻辑表达式,反之亦然。但重要的是您的客户理解规范中的代数 and/or 逻辑,并且您的程序员可以编写等效的 SQL 表达式。
与其如此自然地将表示参数的 table 连接到表示函数的 table,您还可以 1) 交叉连接每一行 table 持有将参数 table 限制为给出该结果的行的函数结果然后 2) 合并交叉连接。
(restrict SPECIES=dog Animals) natural join TABLE{ISDOG}{<1>}
union (restrict SPECIES<>dog Animals) natural join TABLE{ISDOG}{<0>}
另请参阅:Is multiplication allowed in relational algebra?
重新查询关系:Relational algebra for banking scenario
重新理解关系代数的语义 & SQL(加上更多链接):Is there any rule of thumb to construct SQL query from a human-readable description?
PS SQL SELECT 做投影做的事情,但它也做其他不是投影的事情,这得到通过重命名在代数中完成,加入 and/or table 文字。你想要的不是投影。它被作者 Chris Date 称为 EXTEND。我会建议任何人 use/reference Date 的代数。尽管在任意逻辑表达式(wffs 和术语)上添加 RESTRICT/WHERE 和 EXTEND 回避了如何以代数方式处理逻辑表达式的问题。这个答案解释了 that/how 你总是可以代数地表达给定文字 and/or 运算符 tables.
的逻辑表达式
假设我有一个 table 'animals',其行代表不同的动物,并且有一列 species 可能具有 'cat'、'dog'、[=19 等值=], 'cow' 等。假设我只对动物是否是狗感兴趣。在 sql 中(至少在 MySQL 中)我可以像 isDog from animals[=15] 那样查询 select (species='dog') =] 到 return 狗为 1,否则为 0。我如何在 RA 中表达这个?它不是 selecting 因为我们没有限制行数。即使我的表达式 (species='dog') 本身不是属性,我也可以使用项目运算符吗?或者我该如何处理?
编辑: 我想通过在不存在但基于语句真值的列上使用项目运算符来实现结果。例如,table 只包含一列的动物行 'species' 有行:猫、狗、马、牛。我需要可以重命名为 'isDog' 的布尔值,这将导致值 0,1,0,0(1=true,0=false)。我通过 selecting (species='dog') 作为 isDog 在 MySQL 中获得此信息,我想知道将项目操作员与 (species='dog') 选择这样一个动态创建的列,还是有其他方法来处理这个问题?
TL;DR 要将特定值引入关系代数表达式,您必须有一种编写 table 文字的方法。通常必要的运算符没有明确说明,但另一方面,代数练习经常使用 一些 种符号来表示示例值。
对于您的情况,使用最简单的附加关系(使用类似于 SQL VALUES 的临时 table 文字表示法):
(restrict SPECIES=dog Animals) natural join TABLE{ISDOG}{<1>}
union (restrict SPECIES<>dog Animals) natural join TABLE{ISDOG}{<0>}
如果您想参考具有用于一般计算的运算符的关系代数,请参阅作者 Chris Date 的 EXTEND 运算符和他的经典教科书数据库系统简介,第 8 版。
(更一般地说:要输出一个 table,其中包含新列的行,其值是输入行中值的任意函数,我们必须能够访问对应于运算符的 tables。要么我们使用 table 文字或者我们假设一个布尔函数并将其名称用作 table 名称。)
但事实证明关系模型的设计是为了:
每个代数运算符对应一个特定的逻辑运算符。
NATURAL JOIN
&AND
RESTRICT
theta
&AND
theta
UNION
&OR
MINUS
&AND NOT
PROJECT
all but
C
&EXISTS C
etc
每一个嵌套的代数表达式对应一个特定的嵌套逻辑表达式。
Animals
&
"animal named NAME is AGE years old ... and is of species SPECIES"
restrict SPECIES=dog Animals
&
"animal named NAME is AGE years old ... and is of species SPECIES" AND SPECIES=dog
一个命题是一个陈述。 predicate 是一个语句模板。如果每个基数 table 包含由其列参数化的谓词构成真命题的行,则查询包含由其列参数化的相应谓词构成真命题的行。
-- table of rows where animal named NAME is AGE years old ... and is of species SPECIES Animals -- table of rows where animal named NAME is AGE years old ... and is of species SPECIES AND if SPECIES=dog then ISDOG=1 ELSE ISDOG=0 -- ie rows where animal named NAME is AGE years old ... and is of species SPECIES AND (SPECIES=dog AND ISDOG=1 OR SPECIES<>DOG AND ISDOG=0) -- ie rows where animal named NAME is AGE years old ... and is of species SPECIES AND SPECIES=dog AND ISDOG=1 OR animal named NAME is AGE years old ... and is of species SPECIES AND SPECIES<>dog AND ISDOG=0 (restrict SPECIES=dog Animals) natural join TABLE{ISDOG}{<1>} union (restrict SPECIES<>dog Animals) natural join TABLE{ISDOG}{<0>}
所以你可以只用逻辑,工程(包括软件)、科学(包括计算机)和数学中精确的语言来描述你的结果tables。
-- table of rows where animal named NAME is AGE years old ... and is of species SPECIES AND ISDOG=(if SPECIES=dog then 1 else 0)
因此您可以在规范中使用 table 表达式 and/or 逻辑表达式,以(子)表达式逐(子)表达式为基础,以更清晰的为准.
Animals natural join table of rows where if SPECIES=dog THEN ISDOG=1 ELSE ISDOG=0
(对应于该 IF 表达式的 table 每个字符串都有一行,而 'dog' 行是唯一带有 1 的行。)
(Nb SQL ON & WHERE 左边有这种形式的 a table,右边有 predicate with functions。)
- 代数表达式计算满足其对应逻辑表达式的行。
你可能不太清楚什么 等价关系表达式对应于什么等价逻辑表达式,反之亦然。但重要的是您的客户理解规范中的代数 and/or 逻辑,并且您的程序员可以编写等效的 SQL 表达式。
与其如此自然地将表示参数的 table 连接到表示函数的 table,您还可以 1) 交叉连接每一行 table 持有将参数 table 限制为给出该结果的行的函数结果然后 2) 合并交叉连接。
(restrict SPECIES=dog Animals) natural join TABLE{ISDOG}{<1>} union (restrict SPECIES<>dog Animals) natural join TABLE{ISDOG}{<0>}
另请参阅:Is multiplication allowed in relational algebra?
重新查询关系:Relational algebra for banking scenario
重新理解关系代数的语义 & SQL(加上更多链接):Is there any rule of thumb to construct SQL query from a human-readable description?
PS SQL SELECT 做投影做的事情,但它也做其他不是投影的事情,这得到通过重命名在代数中完成,加入 and/or table 文字。你想要的不是投影。它被作者 Chris Date 称为 EXTEND。我会建议任何人 use/reference Date 的代数。尽管在任意逻辑表达式(wffs 和术语)上添加 RESTRICT/WHERE 和 EXTEND 回避了如何以代数方式处理逻辑表达式的问题。这个答案解释了 that/how 你总是可以代数地表达给定文字 and/or 运算符 tables.
的逻辑表达式