Search code examples
relational-databaserelational-algebrarelational

Why can't I compare 2 tables with Different number of Tuples in Relational?


A Question came up in class and I am not sure I am understanding the answer. In the program relational https://ltworf.github.io/relational/ , when using the ∩ operator, you MUST have tables with the same number of Tuples ( My teacher said like comparing apples to apples, not apples to oranges ). However, if I run a query on just 1 column in 2 Tables ( like city in a user database, and city in a storeLocation DB ), why is it required that the tables have the same number of Tuples?

If I want to compare City in 2 tables, why does each table need to have the same tuples. Why can't I just query on city without having to worry about the number of tuples in the table.


Solution

  • For you to be able to use the normal set operators (- ∪ ∩) on relations, they need to be the same "kind" of relation. That is, they need to have the same columns.

    To achieve that, you need to use projection and rename.

    So if you have a relation A with fields id, name and you want to do a union with a relation B which has id, first_name, last_name you need to do: A ∪ πid, name (ρ first_name ➡name (B))