6

I'm a total begginer in Relational Algebra and I don't manage to fully understand how cartesian product works.

I want to know what happen in cartesian product when my two table have common attributes. I have no problem to understand when both table don't have any attribute in common.

For instance I tried to understand on example I made myself.

T1                          T2
----------                  -----------
A   B   C                   A    B   D
1   4   7                   1    4   8
2   5   3                   2    9   5
4   7   1                   7    3   2

If I want to do T1 x T2, when I want to write the line that is the concatenation of the first line of T1 A=1 , B=4 , C=7 and the second line of T2 A=2 ,B=9 ,C=5 , what happen to the column A and B ?

If you could show me the result of T1 x T2 it would be really useful!

Peni
  • 626
  • 1
  • 7
  • 18
  • Please give a reference to your relational algebra variant. Many algebras have relations with ordered attributes, which can have duplicate names; so they can calculate a product of any two relations. – philipxy Sep 20 '17 at 21:19
  • Possible duplicate of [Cartesian products of tables that contains same columns](https://stackoverflow.com/questions/33942237/cartesian-products-of-tables-that-contains-same-columns) – philipxy Sep 20 '17 at 21:23

1 Answers1

7

Edit: As discussed on comment section. I'm editing this answer for a proper understanding of the question.

As this is a possible duplicate, refer to this link: Cartesian products of tables that contains same columns

First, it depends on the algebra that you are using. For some, the cartesian product can be done between tables with common attributes, similar to a cross join, and on other algebras the cartesian product is not permitted, so you would have to rename the attributes.

p.s: Look up Cross Joins as they are similar to a cartesian product but with no conditions to join, see expected result of a cross join of your example tables:

    T1                      T2                T1 X T2
----------              -----------     =    ----------------------------
A   B   C                A    B   D          T1.A T1.B T1.C T2.A T2.B T2.D
1   4   7                1    4   8           1     4   7    1    4    8
2   5   3                2    9   5           1     4   7    2    9    5
4   7   1                7    3   2           1     4   7    7    3    2
                                              2     5   3    1    4    8
                                              2     5   3    2    9    5
                                              2     5   3    7    3    2
                                              4     7   1    1    4    8
                                              4     7   1    2    9    5
                                              4     7   1    7    3    2 

refer link: What is the difference between Cartesian product and cross join?

Also, i highly recommend you to check this out and try it yourself!

RelaX - relational algebra calculator http://dbis-uibk.github.io/relax/index.htm

  • 1
    When i try my example in the calculator you gave me I get `join would result in non unique column names; the following columns appear in both relations: A, B`. So Should I conclude that I can't make a cartesian product without renaming A and B ? – Peni Sep 20 '17 at 12:17
  • Oh snap! I misunderstood your question. I'll edit my answer. But to correct it here already. Cartesian product is a case of natural join where the joined relations don't have any attribute names in common. What i misunderstood for a Cross Join, which then would give you some valid result. And, yes, you should conclude that. – Victor Medeiros Sep 20 '17 at 12:24
  • Errr I think the fault is mine I dindn"t know there were a difference beetweens Cross Product and Cartesian Product! But what i meant was cross product and that's what i used on the calculator (Named cross join on the website) ! And it gave me the error message i wrote before. – Peni Sep 20 '17 at 12:30
  • Strange. I used the same data you provided as example here and it gave a valid result: [Printscreen 1](https://puu.sh/xEnGB/f1dffda181.png) [Printscreen 2](https://puu.sh/xEnGW/0d4997d479.png) – Victor Medeiros Sep 20 '17 at 12:36
  • @Peni & VictorMedeiros Depends on the algebra variant. See my comments on the question. – philipxy Sep 20 '17 at 21:25
  • @philipxy Yes! On my answer i refer to the same link. I'm kinda new here so i didn't remember to mark as possible duplicate. – Victor Medeiros Sep 20 '17 at 22:04
  • 1
    @Peni & VictorMedeiros Ha ha missed a link to my own answer. Notice that that link explains that this answer is only correct for *certain algebras*, and for others wrong, hence is misleading, and the question doesn't give an algebra, so I wish the asker good luck. I find "more technical" odd--it's just more comprehensive since the question (like this one) is general. The need for a lot of cases & notions is because so many "algebras" are ridiculously complicated ([and sometimes not "algebras" at all](https://stackoverflow.com/a/46321855/3404097)). Generally motivated by SQL apologetics. – philipxy Sep 20 '17 at 22:35
  • You are completely correct, i'll edit it so it's not so confusing. – Victor Medeiros Sep 20 '17 at 22:56