Question

I have 3 different tables, with each only having one field.

|fname1 |    |fname2 |    |fname3 |
|*******|    |*******|    |*******|
|Adam   |    |Andy   |    |Alan   |
|Bob    |    |Bryan  |    |David  |
|Charles|    |Chuck  |    |Shawn  |

I want to merge these tables into one big table with only an "fname" field as follows:

|fname  |
|*******|
|Adam   |
|Bob    |
|Charles|
|Andy   |
|Bryan  |
|Chuck  |
|Alan   |
|David  |
|Shawn  |

This is pretty simple in SQL, but I cannot for the life of me figure out how one would write this in relational algebra. I do not care about the order, only that all the data is in one big table under one field.

Was it helpful?

Solution

In theory, relational algebra deals with sets. A set will never have duplicates.

In practice relational dbms do not implement set-based relational algebra. One of the reasons is performance. Every query would have to remove duplicates (which is an expensive operation) so in practice, DBMS's deal with multi-sets: where duplicate tuples are allowed.

This imposes a challenge: SQL does not really have a good way to deal with duplicate tuples. For example, assume you have a table with one attribute, and two tuples, both identical. Now remove the first tuple and leave the second one. You can't do that without some type of hack.

So back to your question: the answer to your question is UNION.

R1 U R2 U R3

it does not matter in relational algebra if there are duplicates, because in theory, you can't have duplicates in sets.

--dmg

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top