在不使用聚合函数的情况下查询关系代数

时间:2013-06-11 19:16:33

标签: database aggregate-functions relational-algebra

主题数据库系统考试的任务:

我有以下架构:

Excavator(EID, Type) - EID is a key
Company(Name, HQLocation) - Name is a key
Work(Name, EID, Site, Date) - All collumns together form a key

我必须在关系代数中编写这个查询:

"Which company was digging on exactly one site on 1st of May?"

我不知道如何在没有聚合函数(count)的情况下表达它。我知道人们将这些功能添加到关系代数中,但在本次考试中我们被禁止这样做。

您可以使用标准集操作,除法,投影,选择,连接,笛卡尔积。

1 个答案:

答案 0 :(得分:4)

我现在忘记了正确的关系代数语法,但你可以做到

       (Worked on >= 1 site on 1st May) 
minus  (Worked on > 1 site on 1st May)
--------------------------------------
equals (Worked on 1 site on 1st May)

仅使用评论中提到的运算符(并假设为rename)的SQL解决方案如下所示。

SELECT Name
FROM   Work
WHERE  Date = '1st May' /*Worked on at least one site on 1st May */

EXCEPT

SELECT W1.Name /*Worked more than one site on 1st May */
FROM   Work W1
       CROSS JOIN Work W2
WHERE  W1.Name = W2.Name
       AND W1.Date = '1st May'
       AND W2.Date = '1st May'
       AND W2.Site <> W2.Site 

我认为这将是相对简单的翻译