15
http://www.cse.msu.edu/ http://www.cse.msu.edu/ ~cse103 ~cse103 U:/msu/course/cse/103 U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 1 CSE 103 CSE 103 Students: Students: Review days 7 and 8 if you need to go Review days 7 and 8 if you need to go over relationships and INNER JOINs again over relationships and INNER JOINs again Can also review Day 7 Notes on web page Can also review Day 7 Notes on web page Read Day 11 for INNER vs. OUTER JOIN Read Day 11 for INNER vs. OUTER JOIN Today we will continue with INNER JOINs Today we will continue with INNER JOINs and ultimately move on to OUTER JOIN as and ultimately move on to OUTER JOIN as well well Others: Others: Please save your work and log out ASAP. Please save your work and log out ASAP.

U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

Embed Size (px)

DESCRIPTION

U:/msu/course/cse/103 Day 08, Slide 3 INNER JOINs Inner Join – combines records from two tables whenever there are matching values in a matched field –e.g., SELECT MovieID, Role FROM tbl_Roles INNER JOIN tbl_People ON ActorID = PersonID WHERE FirstName = "Matt" AND LastName = "Damon" –With multiple tables, duplicate fieldnames can occur. Use tbl_name.fieldname to clarify. NULL never matches

Citation preview

Page 1: U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 11

CSE 103CSE 103

Students:Students:– Review days 7 and 8 if you need to go over Review days 7 and 8 if you need to go over

relationships and INNER JOINs againrelationships and INNER JOINs againCan also review Day 7 Notes on web pageCan also review Day 7 Notes on web page

– Read Day 11 for INNER vs. OUTER JOINRead Day 11 for INNER vs. OUTER JOIN– Today we will continue with INNER JOINs and Today we will continue with INNER JOINs and

ultimately move on to OUTER JOIN as wellultimately move on to OUTER JOIN as well

Others: Others: – Please save your work and log out ASAP.Please save your work and log out ASAP.

Page 2: U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 22

Homework DebriefHomework Debrief

What are the three types of relationships What are the three types of relationships and what are some examples of each from and what are some examples of each from our databases?our databases?When does the JOIN keyword need to When does the JOIN keyword need to appear in an SQL statement?appear in an SQL statement?What are the different types of JOINS?What are the different types of JOINS?

Page 3: U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 33

INNER JOINsINNER JOINsInner JoinInner Join – combines records from two – combines records from two tables whenever there are matching tables whenever there are matching values in a matched fieldvalues in a matched field– e.g., SELECT MovieID, Role FROM tbl_Roles e.g., SELECT MovieID, Role FROM tbl_Roles

INNER JOIN tbl_People ON ActorID = INNER JOIN tbl_People ON ActorID = PersonID WHERE FirstName = "Matt" AND PersonID WHERE FirstName = "Matt" AND LastName = "Damon"LastName = "Damon"

– With multiple tables, duplicate fieldnames can With multiple tables, duplicate fieldnames can occur. Use tbl_name.fieldname to clarify.occur. Use tbl_name.fieldname to clarify.

NULL never matchesNULL never matches

Page 4: U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 44

Homework problemHomework problem

What type of Movie was more common in 2004, What type of Movie was more common in 2004, ACT (Action) or COM (Comedy)?ACT (Action) or COM (Comedy)?– Use two queriesUse two queriesWhat tables do we need?What tables do we need?– How are the tables related?How are the tables related?– How do we write the FROM line?How do we write the FROM line?What other restrictions do we need?What other restrictions do we need?– How do we enforce these?How do we enforce these?How can we easily write the second query once How can we easily write the second query once the first is done?the first is done?

Page 5: U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 55

Multiple-table queriesMultiple-table queriesin Design Viewin Design View

In Design view for a query, ADD all the In Design view for a query, ADD all the tables you will needtables you will needScroll each table's list of fields so the Scroll each table's list of fields so the related fields are visiblerelated fields are visibleDrag one field onto the other (direction Drag one field onto the other (direction doesn't matter)doesn't matter)Tables are now related via INNER JOIN Tables are now related via INNER JOIN Setup rest of query as beforeSetup rest of query as before

Page 6: U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 66

Multiple-table queries in Design Multiple-table queries in Design View: ExerciseView: Exercise

Using the music-day08 database (copied Using the music-day08 database (copied to your P:) construct the following queries:to your P:) construct the following queries:– List album titles and years with their label List album titles and years with their label

name name [42][42] (qry_day08_albums_with_labels)(qry_day08_albums_with_labels)– List the digital tracks with their track title, List the digital tracks with their track title,

duration, and digital filesize duration, and digital filesize [362][362](qry_day08_digital_tracksizes)(qry_day08_digital_tracksizes)

– List the tracks in the database (track numberList the tracks in the database (track numberand title) with the name of their secondaryand title) with the name of their secondaryartist artist [9][9] (qry_day08_secondary_artists)(qry_day08_secondary_artists)

Page 7: U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 77

INNER JOINs and table aliasesINNER JOINs and table aliases

What SQL Keyword is used for aliases What SQL Keyword is used for aliases (remember calculated fields)?(remember calculated fields)?Same idea, only now rename tables to make Same idea, only now rename tables to make JOINs easierJOINs easier– Also enables using same table twice, as in self-joinAlso enables using same table twice, as in self-joinUse table alias throughout ENTIRE query:Use table alias throughout ENTIRE query:SELECT MovieTitle, SELECT MovieTitle, rr.MovieID, Role FROM .MovieID, Role FROM tbl_Roles tbl_Roles AS rAS r INNER JOIN tbl_Movies INNER JOIN tbl_Movies AS mAS m ON ON rr.MovieID = .MovieID = mm.MovieID WHERE MovieTitle .MovieID WHERE MovieTitle = "Blues Brothers, The"= "Blues Brothers, The"

Page 8: U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 88

Pencil and Paper Exercise on Pencil and Paper Exercise on Relation TablesRelation Tables

Does it matter which tables we join first?Does it matter which tables we join first?What does the shaded area in the upper What does the shaded area in the upper left-hand corner of the worksheet left-hand corner of the worksheet represent?represent?Which two tables should be joined next to Which two tables should be joined next to complete the relational JOIN table?complete the relational JOIN table?How many rows does the resulting JOIN How many rows does the resulting JOIN have?have?

Page 9: U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 99

Relationships in MoviesRelationships in Movies

Work in your groups to identify Work in your groups to identify relationships in the movies databaserelationships in the movies database– For each pair of tables, choose 1:1, 1:N, N:M, For each pair of tables, choose 1:1, 1:N, N:M,

or not relatedor not related– Look at primary keys; find where they're used Look at primary keys; find where they're used

as foreign keysas foreign keys– Use common sense… think "Does 1 X have Use common sense… think "Does 1 X have

many Y?"many Y?"But always check that the keys support this idea!But always check that the keys support this idea!

Page 10: U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 1010

Multiple-table Queries in MoviesMultiple-table Queries in Movies– Create a list of all the movies (titles and Create a list of all the movies (titles and

years) in which Robert Redford has appeared. years) in which Robert Redford has appeared. [62][62]

– Create a list of all the directors who have Create a list of all the directors who have made “Frankenstein” movies, to include the made “Frankenstein” movies, to include the title, year, and director’s name. title, year, and director’s name. [68][68]

– Create a list of everyone who appeared in any Create a list of everyone who appeared in any movie made after 1990 who has since diedmovie made after 1990 who has since died [1961 with duplicates][1961 with duplicates]

How can we remove duplicates?How can we remove duplicates? [595] [595]

Page 11: U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 1111

Matching partial data:Matching partial data:SQL SQL OUTER JOIN…ONOUTER JOIN…ON

Outer JoinOuter Join– Useful with relationships where not all X have some property YUseful with relationships where not all X have some property Y

1:1s always have this: not all tracks have a digital track; not all 1:1s always have this: not all tracks have a digital track; not all movies have a ratingmovies have a ratingSome 1:Ns/M:Ns – not every person has an acting role; not every Some 1:Ns/M:Ns – not every person has an acting role; not every album has a label (because our data is incomplete)album has a label (because our data is incomplete)

LEFT OUTER JOIN – combines records from two tables, LEFT OUTER JOIN – combines records from two tables, but shows all records from the but shows all records from the leftleft table even if there are table even if there are no matches with the right tableno matches with the right table

e.g., All tracks, showing secondary artists where present: …e.g., All tracks, showing secondary artists where present: …tbl_Tracks tbl_Tracks LEFT JOINLEFT JOIN tbl_Artists tbl_Artists ONON [Secondary Artist] = ArtistID [Secondary Artist] = ArtistID

Right Outer Join – (likewise … Right Outer Join – (likewise … rightright table ….) table ….)Fields in the unmatched table(s) are set to NULLFields in the unmatched table(s) are set to NULL

Page 12: U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 1212

OUTER JOIN in Design ViewOUTER JOIN in Design View

Setup the query as if you were using an Setup the query as if you were using an INNER JOININNER JOINDouble-click the relationship to changeDouble-click the relationship to change– Look at options: first always corresponds to Look at options: first always corresponds to

INNER JOIN (matched records only)INNER JOIN (matched records only)– Second and third correspond to LEFT and Second and third correspond to LEFT and

RIGHT, but read carefully to see which table RIGHT, but read carefully to see which table you want to include ALL fromyou want to include ALL from

Access will use LEFT or RIGHT correctly; you Access will use LEFT or RIGHT correctly; you don't have to know which is whichdon't have to know which is which

Page 13: U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 1313

Music-day08 Multi-table queriesMusic-day08 Multi-table queries

Construct a list of Tracks with Track and Album Construct a list of Tracks with Track and Album Titles, arranged by album [376] Titles, arranged by album [376] qry_day08_titlesqry_day08_titlesUse that as a starting point for the next few Use that as a starting point for the next few queries (start over each time): queries (start over each time): – Add secondary artist names, still listing all tracks Add secondary artist names, still listing all tracks

(qry_day08_titles_with_2(qry_day08_titles_with_2ndnd_artist)_artist)– Add labels for all tracks, even if unknown Add labels for all tracks, even if unknown

(qry_day08_titles_with_labels)(qry_day08_titles_with_labels)– Find tracks that have no digital tracks [14] Find tracks that have no digital tracks [14]

(qry_day08_titles_missing_digitracks)(qry_day08_titles_missing_digitracks)

Page 14: U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 1414

Bridge Task 1.5Bridge Task 1.5Everyone should comeEveryone should come– Get next BT not passed, either 1.0 or 1.5Get next BT not passed, either 1.0 or 1.5

Primarily on material from Days 6-8Primarily on material from Days 6-8– Action queries and importing, appendAction queries and importing, append– INNER and OUTER JOIN in SQL and AccessINNER and OUTER JOIN in SQL and Access– Be sure homework is all doneBe sure homework is all done

Have copies of databases we used in class Have copies of databases we used in class and homework:and homework:– music_demo, music-day08, colleges&universitiesmusic_demo, music-day08, colleges&universities

Bring photo ID!Bring photo ID!

Page 15: U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 08, Slide Day 08, Slide 1515

HomeworkHomework

Prepare for BT 1.5Prepare for BT 1.5– Make sure that you have completed ALL Make sure that you have completed ALL

imports into the various databases!imports into the various databases!Practice queries… ask questions like Practice queries… ask questions like "Who starred in Action movies in 2003?" "Who starred in Action movies in 2003?" and write queries to solve themand write queries to solve themReread textbook chapters 6-8 and 11Reread textbook chapters 6-8 and 11Understand relationships in our databases Understand relationships in our databases so you can join tables correctlyso you can join tables correctly