TRC: Select an ID if it appears on all measurements

database-theoryrelational-calculusrelational-theory

I have these two "tables":

Station (station_id, city)            // Stations details
Rainfall (station_id, date, amount)   // Rainfall measurements

With Tuple Relational Calculus (TRC), I need to get the IDs of all stations that have related measurements on every date in which any measurement was taken.

So with the following tables I'm supposed to select 5 because this station has measurements on every date that has a measurement.

       Station                             Rainfall
       -------                             --------
--station_id-----city--      --station_id-----date-----amount--
      5           LA               5          01/01      4
      7           NY               7          02/02      8
                                   5          02/02      3

I've tried this but I'm not quite sure if this would work if there are no measurements at all (in this case I'm supposed to select all station IDs):

{ t | ∃s ∈ Station (t[station_id] = s[station_id] ∧ 
    ∀r ∈ Rainfall (∃q ∈ Rainfall (r[date] = q[date] ∧ s[station_id] = q[station_id])) ) }

I need your help in understanding if what I did is correct.

Best Answer

I had to look up wikipedia because I did not know Tuple Relational Calculus. I did only skim over that article. There are differnces in notation as far as I can see.

But after activating my knowledge about formal logic I think you are right because

∀r ∈ Rainfall (...)

is true if Rainfall does not contain any tuples. In this case your query reduces to

{ t | ∃s ∈ Station (t[station_id] = s[station_id]}

and this describes all tuples of Station

What I am missing in your notation is that is unclear which attributes the tuple t of the solution contains. So if you want to select station_id then the query should start with something like

{ t: {station_id} | ...}

if one uses the notataion of the wiki article.

Maybe you can supply some reference to your notation.