1

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

我有以下架构:

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?"

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

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

4

1 回答 1

5

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

       (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 

我认为这将是相对直接的翻译

于 2013-06-11T20:01:46.790 回答