Cartesian products of tables that contains same columns
Asked Answered
B

2

2

I am really confused about this. I searched a lot of tutorials but i couldnot find a clear answer.

A  B        B  D
1  X        x  5
2  x        y  6
            x  4

I want to cross this two tables.A , B, B,d are attributes.

A  B   B  D
1  X   x  5
2  x   x  5
1  X   y  6
2  x   y  6
1  X   x  4
2  x   x  4

This should be answer normally according to rule of cartesian. Cross all rows. But i am confused about same column B. Same column will seem twice?

Bettyebettzel answered 26/11, 2015 at 15:31 Comment(1)
There are many RAs (relational algebras). They differ in operators & even what a relation is. Give a reference and/or definition for yours. Eg textbook name, edition & page.Ordonez
O
2

Some relational query language/algebra relations have ordered column names. So an attribute name can name more than one column. (There should be a way to reference a column by column number.) The Cartesian product operator can take any two relations, because it is not a problem for an attribute name to appear more than once on output (or input).

Some relational query language/algebra Cartesian product operators change attribute names on output. Input relations have associated names. (The input either includes names of relation variables/constants or relation values contain names.) Each output column name is a combination of an input column name and an input relation name. The Cartesian product operator can take any two relations. Because columns that appear in different relations with the same attribute name on input will be distinguished by input relation name on output.

Some relational query languages/algebras have both. The Cartesian product operator can take any two relations.

Some relational query language/algebra relations have only one copy of an attribute name. So Cartesian product can only be called when inputs don't share any attribute names. (It is just a special case of NATURAL JOIN and/or EQUIJOIN.) If you have inputs sharing column names and you want output with a column for every input column then you need to rename columns so all are different before you join.

So you must pay close attention to what kind of relations and operators a relational query language/algebra has.

PS: SQL query language

The FROM clause of a SELECT expression makes a temporary Cartesian product of its tables. Every column name is an input column name prefixed by a given table name or table alias, plus a dot. This is the changes-names verions of Cartesian product above. The dotted names are used in the rest of the SELECT expression. The SELECT clause finally removes the prefixes and dots. So there are no dots in an SQL table value outside a SELECT expression. (The columns are also ordered, and there can be duplicate column names.)

If your SQL table expressions are AB & BD then you just need

SELECT * FROM AB ab CROSS JOIN BD bd
Ordonez answered 29/11, 2015 at 3:49 Comment(4)
so my answer as output is true, i couldnot understand yours sorryBettyebettzel
I cannot tell whether your answer is correct. There are manu versions of relational algebra. Please give a reference for the relational algebra that you are told to use. Make sure you understand that algebra's relations and operators. Also: Do you mean that you don't understand my answer? Or only my code?Ordonez
I understand code. We dont need code for algebra.It is standart algebra. cs.helsinki.fi/u/laine/tkpv/algebra/buildingqueries.html like this.Bettyebettzel
You just accepted my answer. I just edited it. I hope it is clearer. PS The whole point of my answer is that there is no one standard algebra. PPS Can you give a link that defines what a relation is.Ordonez
C
1

Dont you just need the following;

SELECT * FROM TAbleA CROSS JOIN TableB

?

Copaiba answered 26/11, 2015 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.