Re: Relational Algebra Expression

From: JOG <jog_at_cs.nott.ac.uk>
Date: Sat, 9 Feb 2008 14:16:38 -0800 (PST)
Message-ID: <3e9f3ead-f83d-44a7-8477-927bf6e859c8_at_e25g2000prg.googlegroups.com>


On Feb 9, 4:59 pm, gamehack <gameh..._at_gmail.com> wrote:
> On Feb 9, 4:47 pm, Bob Badour <bbad..._at_pei.sympatico.ca> wrote:
>
> > gamehack wrote:
> > [snip]
> > > What I'm trying to do is extract all the years where we have more than
> > > 1 relation for the year. For the sample table, we need to get:
> > > 1999
> > > 2001
>
> > > I tried to do in a couple of ways but I couldn't. I tried using a
> > > projection on Year so that I can remove duplicates but then I can't
> > > just use difference because the new relations are not compatible. Any
> > > hints are greatly appreciated.
>
> > Equijoin on year and inequality theta-join on name project on year.
>
> Hi,
>
> I can't really understand what this means - can you bracket it so I
> can see the results of each operation?
>
> Thanks very much,
> g

Well Bob gave you everything you needed, but I guess you're learning the stuff at the moment (coursework?) so lets break down his instructions:

  1. EQUIJOIN R with itself (renamed B) where Year = B.Year
  2. RESTRICT where Name != B.Name
  3. PROJECT on Year

In terms of whats going on:
1 - Gives you a relation of any two rows with the same year concatenated together
2 - Removes the years that were joined with themselves in 1. 3 - Gets rid of all attributes apart from Year. Because a relation is a set this also eliminates any duplicates, and voila you are left with the years that appeared more than once.

Note if you use SQL it can allow duplicates (which is of course particularly brain-dead given a relation is a set), so you have to specify you want distinct tuples:

SELECT DISTINCT Year FROM R, R as B
WHERE R.Year = B.Year AND R.Name != B.Name Received on Sat Feb 09 2008 - 23:16:38 CET

Original text of this message