将SQL转换为关系代数示例

时间:2016-10-10 18:51:29

标签: sql database relational-algebra

我编写了这个SQL语句,想要找出它的关系代数:

SELECT S.Oid
FROM Shipment S
WHERE EXISTS (SELECT W.Wid
FROM Warehouse W
WHERE W.City = ‘Pittsburgh’ AND S.Wid = W.Wid)

这是声明的正确关系代数吗?

  

(πSoO(发货S))U(πW.City='Pittsburgh',S.Wid = W.Wid   (仓库W))

1 个答案:

答案 0 :(得分:2)

您的代数表达式不正确。

要找到正确答案,您可以注意到SQL查询等同于连接:

SELECT DISTINCT S.Oid
FROM Shipment S, Warehouse W
WHERE S.Wid = W.Wid AND W.City = ‘Pittsburgh’

所以在关系代数中,这相当于:

π S.Oid (装运s⨝ S.Wid = W.Wid σ W.City ='匹兹堡'(仓库W ))