表示关系代数中的子查询
如何表示关系代数中的子查询?我是否将新的选择放在先前的选择条件下?
SELECT number
FROM collection
WHERE number = (SELECT anotherNumber FROM anotherStack);
How do I represent a subquery in relation algebra? Do I put the new select under the previous select condition?
SELECT number
FROM collection
WHERE number = (SELECT anotherNumber FROM anotherStack);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需将其重写为
join
即可。我不确定我在关系代数中学到的语法在语言中的使用有多广泛。
anotherStack
获取anotherNumber
的投影anotherNumber
重命名为number
collection
number
的最终投影You would just rewrite that as a
join
.I'm not sure how widely used the syntax I learned for Relational Algebra is so in words.
anotherNumber
fromanotherStack
anotherNumber
from the result of step 1 asnumber
collection
number
from the result of step 3答案取决于你的代数包含哪些运算符。半连接运算符在这里最有用。
如果两个关系中的公共属性都被命名为
number
,那么它将是一个半连接,后跟number
的投影。假设一个名为MATCHING
的 sem-join 运算符,按照教程 D< /a>:正如发布的那样,首先需要重命名该属性:
如果标准 SQL 的 (SQL-92)
JOIN
可以被认为是关系运算符,那么 SQL 确实没有 no半连接。然而,它有几个可用于编写半连接运算符的比较谓词,例如MATCH
:然而,
MATCH
在现实生活中的 SQL 产品中并未得到广泛支持,因此为什么半连接通常使用IN (子查询)
或EXISTS (子查询)
编写(我怀疑这就是您在问题中检查“子查询”名称的原因,即术语半连接在 SQL 从业者中并不为人所知)。另一种方法是使用相交运算符(如果可用)。
类似于(伪代码):
在 SQL 中:
这在现实生活中得到了很好的支持(SQL Server、Oracle、PostgreSQL 等,但值得注意的是 MySQL 除外)。
The answer depends on which operators your algebra comprises. A semi-join operator would be most useful here.
If the common attribute was named
number
in both relations then it would be a semi-join followed by projection ofnumber
. Assuming a sem-join operator namedMATCHING
, as per Tutorial D:As posted, the attribute needs to be renamed first:
If Standard SQL's (SQL-92)
JOIN
can be considered, loosely speaking, a relational operator then it is true that SQL has no no semi-join. However, it has several comparison predicates that may be used to write a semi-join operator e.g.MATCH
:However,
MATCH
is not widely supported in real life SQL products, hence why a semi-join is commonly written usingIN (subquery)
orEXISTS (subquery)
(and I suspect that's why you name-checked "subquery" in your question i.e. the term semi-join is not well known among SQL practitioners).Another approach would be to use an intersect operator if available.
Something like (pseudocode):
In SQL:
This is quite well supported in real life (SQL Server, Oracle, PostgreSQL, etc but notably not MySQL).
根据这个 pdf,可以转换一个子- 轻松查询关系代数表达式。
首先,您必须将整个查询从形式转换
为
然后您必须首先将子查询转换为关系代数。要对上面给出的子查询执行此操作:
这里
R1, R2...
是上下文关系,r1, r2...
是子查询关系。由于堆栈溢出的语法相当灾难,请前往 该 pdf 可以大致了解如何将子查询转换为关系代数。
According to this pdf, you can convert a sub-query easily to a relational algebric expression.
Firstly, you have to convert the whole query from the form
to
Then you have to convert the sub-query first into relational algebra. To do this for the sub-query given above:
Here
R1, R2...
are the contextual relations, andr1, r2...
are sub-query relations.As the syntax is pretty disaster in stack overflow, please head over to that pdf to get a broad overview of how to convert sub query to relational algebra.