Question

Say that there are the two following tables :

Table1 ( PK1, Attr1 )
Table2 ( PK2, Attr1 )

An SQL statement :

SELECT *
FROM Table1,Table2

result in

( PK1, Table1.Attr1, PK2, Table2.Attr1 ).

And I can't understand why, given the following semantic of the query :

Table1 x Table2

and the definition of the Cartesian Product.

I also would like to know the relational algebra equivalent expression, if any.

This is not homework, I looked up for this with no success and I thank in advance whoever will answer me.

Was it helpful?

Solution

"An SQL statement : SELECT * FROM Table1,Table2

result in

( PK1, Table1.Attr1, PK2, Table2.Attr1 )."

Not true. That SQL statement results in [a table with heading]

( PK1, Attr1, PK2, Attr1 )

Note the difference carefully. In SQL, those dot qualifiers and what precedes them are NOT part of the attribute/column name in the result. They are names for so-called range variables, and these variables exist only inside the SELECT statement in which they appear. Once a SELECT statement is "terminated", those range variables disappear. You can test/verify this by trying

SELECT Table1.Attr1 FROM ( your join here );

Any standard-compliant engine should raise an error on that. Some engines may accept these things because the authors deemed it appropriate to do so, but you should realize that you cannot expect any random engine to behave like this.

You can also try

SELECT Attr1 FROM ( your join here );

But now every engine should be complaining about ambiguous column reference.

So we're back at : that SQL statement results in [a table with heading]

( PK1, Attr1, PK2, Attr1 ).

This is definitely not a relation, hence there is no operation of the relational algebra that can possibly produce this thing, hence there simply does not exist any relational algebra formula that is equivalent to your SQL operation.

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