Saturday, 14 June 2014

DBMS Joins

Post By: Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

DBMS Joins

We understand the benefits of Cartesian product of two relation, which gives us all the possible tuples that are paired together. But Cartesian product might not be feasible for huge relations where number of tuples are in thousands and the attributes of both relations are considerable large.
Join is combination of Cartesian product followed by selection process. Join operation pairs two tuples from different relations if and only if the given join condition is satisfied.
Following section should describe briefly about join types:

Theta (θ) join

θ in Theta join is the join condition. Theta joins combines tuples from different relations provided they satisfy the theta condition.
Notation:
R1 θ R2
R1 and R2 are relations with their attributes (A1, A2, .., An ) and (B1, B2,.. ,Bn) such that no attribute matches that is R1 ∩ R2 = Φ Here θ is condition in form of set of conditions C.
Theta join can use all kinds of comparison operators.
Student
SIDNameStd
101Alex10
102Maria11
[Table: Student Relation]
Subjects
ClassSubject
10Math
10English
11Music
11Sports
[Table: Subjects Relation]
Student_Detail =
STUDENT Student.Std = Subject.Class SUBJECT
Student_detail
SIDNameStdClassSubject
101Alex1010Math
101Alex1010English
102Maria1111Music
102Maria1111Sports
[Table: Output of theta join]

Equi-Join

When Theta join uses only equality comparison operator it is said to be Equi-Join. The above example conrresponds to equi-join

Natural Join ( ⋈ )

Natural join does not use any comparison operator. It does not concatenate the way Cartesian product does. Instead, Natural Join can only be performed if the there is at least one common attribute exists between relation. Those attributes must have same name and domain.
Natural join acts on those matching attributes where the values of attributes in both relation is same.
Courses
CIDCourseDept
CS01DatabaseCS
ME01MechanicsME
EE01ElectronicsEE
[Table: Relation Courses]
HoD
DeptHead
CSAlex
MEMaya
EEMira
[Table: Relation HoD]
Courses ⋈ HoD
DeptCIDCourseHead
CSCS01DatabaseAlex
MEME01MechanicsMaya
EEEE01ElectronicsMira
[Table: Relation Courses ⋈ HoD]

Outer Joins

All joins mentioned above, that is Theta Join, Equi Join and Natural Join are called inner-joins. An inner-join process includes only tuples with matching attributes, rest are discarded in resulting relation. There exists methods by which all tuples of any relation are included in the resulting relation.
There are three kinds of outer joins:

Left outer join ( R  S )

All tuples of Left relation, R, are included in the resulting relation and if there exists tuples in R without any matching tuple in S then the S-attributes of resulting relation are made NULL.
Left
AB
100Database
101Mechanics
102Electronics
[Table: Left Relation]
Right
AB
100Alex
102Maya
104Mira
[Table: Right Relation]
Courses  HoD
ABCD
100Database100Alex
101Mechanics------
102Electronics102Maya
[Table: Left outer join output]

Right outer join: ( R  S )

All tuples of the Right relation, S, are included in the resulting relation and if there exists tuples in S without any matching tuple in R then the R-attributes of resulting relation are made NULL.
Courses  HoD
ABCD
100Database100Alex
102Electronics102Maya
------104Mira
[Table: Right outer join output]

Full outer join: ( R  S)

All tuples of both participating relations are included in the resulting relation and if there no matching tuples for both relations, their respective unmatched attributes are made NULL.
Courses  HoD
ABCD
100Database100Alex
101Mechanics------
102Electronics102Maya
------104Mira
[Table: Full outer join output]

0 comments: