Вопрос

Task from exam for subject Database Systems:

I have following schema:

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

I have to write this query in relational algebra:

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

I don't know how to express it without aggregate functions (count). I know that people add these functions to relational algebra but we were forbidden to do it during this exam.

You can use standart set operations, division, projection, selection, join, cartesian product.

Это было полезно?

Решение

I forget the proper relational algebra syntax now but you can do

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

A SQL solution using only the operators mentioned in the comments (and assuming rename) is below.

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 

I assume this will be relatively straight forward to translate

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top