-1

Considering these 2 tables:

Cd (cd_id, title, no_tracks, date, position, weeks)

Band (band_id, band_name)

The task is: List all the band names and cd titles in Relational Algebra.

I wrote it this way: Π band_name (Band) AND Π title (Cd), however I'm not sure if this is correct.

I was also thinking about Union but I'm unsure.

Charles David
  • 202
  • 2
  • 11

2 Answers2

1

Yes, Union is the way to go, but popovits' answer isn't quite right. In the Relational Algebra, the two operands must be 'Union Compatible' -- that is have the same-named attributes (and those attributes at the same type) -- ref http://en.wikipedia.org/wiki/Relational_algebra, section 'Set operators'. So:

1) Rename the attributes so that they are the same name.

2) You might have to manipulate the values so that they are the same type. (For example: are fields title and band_name the same length?)

Something to note is that if you have a CD title same as a band_name, you'll only get one tuple. (And that scenario is quite likely with self-titled albums).

Note that the RA is more disciplined than SQL. So:

1) If the columns to a SQL UNION are different-named, SQL takes the names from the left operand.

2) SQL also takes the types per the left operand.

(You could get truncation if title is declared shorter than band_name.)

In SQL you can get repeats in the result for self-titled albums, by using UNION ALL http://en.wikipedia.org/wiki/Set_operations_(SQL). Repeats just aren't allowed in RA, because every relation must be a set.

AntC
  • 2,623
  • 1
  • 13
  • 20
-1

This is indeed done with a union.

π title(Cd) ∪ π band_name(Band)
wvdz
  • 16,251
  • 4
  • 53
  • 90