Presentation is loading. Please wait.

Presentation is loading. Please wait.

Joins. Joins In order to maintain normalization in the database design it is necessary to break up data into separate tables. The data can then be re-associated.

Similar presentations


Presentation on theme: "Joins. Joins In order to maintain normalization in the database design it is necessary to break up data into separate tables. The data can then be re-associated."— Presentation transcript:

1 Joins

2 Joins In order to maintain normalization in the database design it is necessary to break up data into separate tables. The data can then be re-associated through the use of a join.

3 What to join? Key columns are the best since these were created for the purpose of existing as a reference.Key columns are the best since these were created for the purpose of existing as a reference. Should have similar dataShould have similar data Should be the same datatypeShould be the same datatype nulls will not join since their value is not known.nulls will not join since their value is not known.

4 Syntax Usually best to put the join conditions first in the WHERE clauseUsually best to put the join conditions first in the WHERE clause Use of aliases greatly simplifies the statement.Use of aliases greatly simplifies the statement. Any logical operator can be used.Any logical operator can be used. A self-join can be performed on the same table by qualifying it twice.A self-join can be performed on the same table by qualifying it twice.

5 Self Join Which authors in Oakland have the same zip code? Select distinct au1.au_fname, au1.au_lname, au1.zip from authors au1, authors au2 where au1.city = “Oakland” and au1.zip = au2.zip and au1.au_id != au2.au_id

6 Outer Joins Allow you to look at the results of a SELECT in the context of what rows did not qualify. Select fname, lname, pubname from author, pub where author.city *= pub.city This will return all the rows of the first table in the statement and with a NULL value in the column of anything that did not qualify from the second. Putting the * on the other side of the = will have the same effect on the second table.

7 Results _au_fname__au_lname_pub_name JohnsonWhiteNULL Marjorie GreenNULL Cheryl CarsonAlgodata Michael O’LearyNULL

8 How a Join is Processed First the system obtains the Cartesian Product of all tables in join Cartesian Product - the matrix of all possible combinations that could satisfy the joinFirst the system obtains the Cartesian Product of all tables in join Cartesian Product - the matrix of all possible combinations that could satisfy the join The select list is used to restrict the columns returned The select list is used to restrict the columns returned The WHERE clause is then used to restrict the rows return that satisfy the queryThe WHERE clause is then used to restrict the rows return that satisfy the query

9 Union clause Way of combining data from multiple queriesWay of combining data from multiple queries Uses the column names from the first SELECT statementUses the column names from the first SELECT statement select fname FirstName, lname LastName, city City from authors where city=‘Oakland’ UNION select fname, lname, city from authors where city=‘New York’ select fname FirstName, lname LastName, city City from authors where city=‘Oakland’ UNION select fname, lname, city from authors where city=‘New York’

10 Union cont. Removes all duplicates by default. Using the ALL keyword prevents this. UNION ALLRemoves all duplicates by default. Using the ALL keyword prevents this. UNION ALL Can use an ORDER BY only in the last SELECT statement which applies to all the outputCan use an ORDER BY only in the last SELECT statement which applies to all the output Can eliminate the need to write numerous SELECT statements.Can eliminate the need to write numerous SELECT statements.


Download ppt "Joins. Joins In order to maintain normalization in the database design it is necessary to break up data into separate tables. The data can then be re-associated."

Similar presentations


Ads by Google