表示关系代数中的子查询

发布于 2024-09-26 02:04:00 字数 153 浏览 4 评论 0原文

如何表示关系代数中的子查询?我是否将新的选择放在先前的选择条件下?

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 技术交流群。

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

发布评论

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

评论(3

情痴 2024-10-03 02:04:00

您只需将其重写为 join 即可。

我不确定我在关系代数中学到的语法在语言中的使用有多广泛。

  1. anotherStack 获取 anotherNumber 的投影
  2. 将步骤 1 的结果中的 anotherNumber 重命名为 number
  3. Natural Join 结果步骤 2 到 collection
  4. 从步骤 3 的结果中获取 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.

  1. Take a projection of anotherNumber from anotherStack
  2. Rename anotherNumber from the result of step 1 as number
  3. Natural Join the result of step 2 onto collection
  4. Take a final projection of number from the result of step 3
救赎№ 2024-10-03 02:04:00

答案取决于你的代数包含哪些运算符。半连接运算符在这里最有用。

如果两个关系中的公共属性都被命名为 number,那么它将是一个半连接,后跟 number 的投影。假设一个名为 MATCHING 的 sem-join 运算符,按照教程 D< /a>:

( collection MATCHING anotherStack ) { number }

正如发布的那样,首先需要重命名该属性:

( collection MATCHING ( anotherStack RENAME { anotherNumber AS number } ) { number }

如果标准 SQL 的 (SQL-92) JOIN 可以被认为是关系运算符,那么 SQL 确实没有 no半连接。然而,它有几个可用于编写半连接运算符的比较谓词,例如 MATCH

SELECT number
  FROM collection
 WHERE MATCH (
              SELECT * 
                FROM collection
               WHERE collection.number = anotherNumber.anotherStack
             );

然而,MATCH 在现实生活中的 SQL 产品中并未得到广泛支持,因此为什么半连接通常使用 IN (子查询)EXISTS (子查询) 编写(我怀疑这就是您在问题中检查“子查询”名称的原因,即术语半连接在 SQL 从业者中并不为人所知)。


另一种方法是使用相交运算符(如果可用)。

类似于(伪代码):

( collection project number ) 
intersect 
( ( anotherStack rename anotherNumber as number ) project number )

在 SQL 中:

SELECT number
  FROM collection
INTERSECT
SELECT anotherNumber
  FROM anotherStack;

这在现实生活中得到了很好的支持(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 of number. Assuming a sem-join operator named MATCHING, as per Tutorial D:

( collection MATCHING anotherStack ) { number }

As posted, the attribute needs to be renamed first:

( collection MATCHING ( anotherStack RENAME { anotherNumber AS number } ) { number }

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:

SELECT number
  FROM collection
 WHERE MATCH (
              SELECT * 
                FROM collection
               WHERE collection.number = anotherNumber.anotherStack
             );

However, MATCH is not widely supported in real life SQL products, hence why a semi-join is commonly written using IN (subquery) or EXISTS (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):

( collection project number ) 
intersect 
( ( anotherStack rename anotherNumber as number ) project number )

In SQL:

SELECT number
  FROM collection
INTERSECT
SELECT anotherNumber
  FROM anotherStack;

This is quite well supported in real life (SQL Server, Oracle, PostgreSQL, etc but notably not MySQL).

黑寡妇 2024-10-03 02:04:00

根据这个 pdf,可以转换一个子- 轻松查询关系代数表达式。

首先,您必须将整个查询从形式转换

SELECT Select-list FROM R1 T1, R2 T2, ...
WHERE
some-column = (
    SELECT some-column-from-sub-query from r1 t1, r2 t2, ...
    WHERE extra-where-clause-if-needed)

SELECT Select-list FROM R1 T1, R2 T2, ...
WHERE
EXISTS (
    SELECT some-column-from-sub-query from r1 t1, r2 t2, ...
    WHERE extra-where-clause-if-needed and some-column = some-column-from-sub-query)

然后您必须首先将子查询转换为关系代数。要对上面给出的子查询执行此操作:

PI[some-column-from-sub-query](
    SIGMA[extra-where-clause-if-needed 
        ^ some-column = some-column-from-sub-query
        ](RO[T1](R1) x RO[T2](R2) x ... x RO[t1](r1) x RO[t2](r2) x ...)
)

这里 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

SELECT Select-list FROM R1 T1, R2 T2, ...
WHERE
some-column = (
    SELECT some-column-from-sub-query from r1 t1, r2 t2, ...
    WHERE extra-where-clause-if-needed)

to

SELECT Select-list FROM R1 T1, R2 T2, ...
WHERE
EXISTS (
    SELECT some-column-from-sub-query from r1 t1, r2 t2, ...
    WHERE extra-where-clause-if-needed and some-column = some-column-from-sub-query)

Then you have to convert the sub-query first into relational algebra. To do this for the sub-query given above:

PI[some-column-from-sub-query](
    SIGMA[extra-where-clause-if-needed 
        ^ some-column = some-column-from-sub-query
        ](RO[T1](R1) x RO[T2](R2) x ... x RO[t1](r1) x RO[t2](r2) x ...)
)

Here R1, R2... are the contextual relations, and r1, 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.

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