科目数据库系统考试任务:
我有以下架构:

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)怎么表达它。我知道人们把这些函数添加到关系代数中,但我们在这次考试中被禁止这样做。
可以使用标准集操作、分割、投影、选择、连接、笛卡尔积。

最佳答案

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

       (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

我认为这将是相对直截了当的翻译

关于database - 在关系代数中查询而不使用聚合函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17051986/

10-12 13:02