Difference between a theta join, equijoin and natural join

后端 未结 7 626
眼角桃花
眼角桃花 2020-11-29 17:20

I\'m having trouble understanding relational algebra when it comes to theta joins, equijoins and natural joins. Could someone please help me better understand it? If I use t

7条回答
  •  生来不讨喜
    2020-11-29 18:05

    @outis's answer is good: concise and correct as regards relations.

    However, the situation is slightly more complicated as regards SQL.

    Consider the usual suppliers and parts database but implemented in SQL:

    SELECT * FROM S NATURAL JOIN SP;
    

    would return a resultset** with columns

    SNO, SNAME, STATUS, CITY, PNO, QTY

    The join is performed on the column with the same name in both tables, SNO. Note that the resultset has six columns and only contains one column for SNO.

    Now consider a theta eqijoin, where the column names for the join must be explicitly specified (plus range variables S and SP are required):

    SELECT * FROM S JOIN SP ON S.SNO = SP.SNO;
    

    The resultset will have seven columns, including two columns for SNO. The names of the resultset are what the SQL Standard refers to as "implementation dependent" but could look like this:

    SNO, SNAME, STATUS, CITY, SNO, PNO, QTY

    or perhaps this

    S.SNO, SNAME, STATUS, CITY, SP.SNO, PNO, QTY

    In other words, NATURAL JOIN in SQL can be considered to remove columns with duplicated names from the resultset (but alas will not remove duplicate rows - you must remember to change SELECT to SELECT DISTINCT yourself).


    ** I don't quite know what the result of SELECT * FROM table_expression; is. I know it is not a relation because, among other reasons, it can have columns with duplicate names or a column with no name. I know it is not a set because, among other reasons, the column order is significant. It's not even a SQL table or SQL table expression. I call it a resultset.

提交回复
热议问题