Represent a subquery in relational algebra

前端 未结 3 653
滥情空心
滥情空心 2020-12-31 20:44

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 =          


        
3条回答
  •  悲&欢浪女
    2020-12-31 21:25

    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.

提交回复
热议问题