Sunday, 5 February 2017

MYSQL JOINING TABLES

Handling Two table Query- Join Query: Some times it is required to access the information from two or more tables, which requires the Joining of two or more tables. Such query is called Join Query. MySQL facilitates you to handle Join Queries. The major types of Join is as follows-  Cross Join (Cartesian Product)  Equi Join  Non-Equi Join  Natural Join :: 77 ::  Cross Join: Consider the two set A= {a,b} and B={1,2} The Cartesian Product i.e. AxB = {(a,1) (a,2) (b,1) (b,2)} Similarly, we may compute Cross Join of two tables by joining each record of first table with each record of second table.  Equvi Join: In Equvi Join, records are joined on the equality condition of Joining Column. Generally, the Join column is a column which is common in both tables. Consider the following table R and S having C as Join column.

Union of tables: Sometimes it is required to combine all records of two tables without having duplicate records. The combining records of two tables are called UNION of tables.UNION Operation is similar to UNION of Set Theory. E.g. If set A= {a,c,m,p,q} and Set B= {b,m,q,t,s} then AUB= {a,c,m,p,q,b,t,s} [All members of Set A and Set B are taken without repeating] The general form to write Union query isSelect …. From [Where ] UNION [ALL] Select …. From [Where ]; Example: Select Ename From PROJECT1 UNIONSelect Ename From PROJECT2 ;  Both tables or output of queries must be UNION compatible i.e. they must be same in column structure (number of columns and data types must be same). 

No comments:

Post a Comment