Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 http://www.cse.msu.edu/~cse103 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 JOINs again Can also review Day 7 Notes on web page –Read Day 11 for INNER vs. OUTER JOIN –Today we will continue with INNER JOINs and ultimately move on to OUTER JOIN as well Others: –Please save your work and log out ASAP.

2 http://www.cse.msu.edu/~cse103 U:/msu/course/cse/103 Day 08, Slide 2 Homework Debrief What are the three types of relationships and what are some examples of each from our databases? When does the JOIN keyword need to appear in an SQL statement? What are the different types of JOINS?

3 http://www.cse.msu.edu/~cse103 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

4 http://www.cse.msu.edu/~cse103 U:/msu/course/cse/103 Day 08, Slide 4 Homework problem What type of Movie was more common in 2004, ACT (Action) or COM (Comedy)? –Use two queries What tables do we need? –How are the tables related? –How do we write the FROM line? What other restrictions do we need? –How do we enforce these? How can we easily write the second query once the first is done?

5 http://www.cse.msu.edu/~cse103 U:/msu/course/cse/103 Day 08, Slide 5 Multiple-table queries in Design View In Design view for a query, ADD all the tables you will need Scroll each table's list of fields so the related fields are visible Drag one field onto the other (direction doesn't matter) Tables are now related via INNER JOIN Setup rest of query as before

6 http://www.cse.msu.edu/~cse103 U:/msu/course/cse/103 Day 08, Slide 6 Multiple-table queries in Design View: Exercise Using the music-day08 database (copied to your P:) construct the following queries: –List album titles and years with their label name [42] (qry_day08_albums_with_labels) –List the digital tracks with their track title, duration, and digital filesize [362] (qry_day08_digital_tracksizes) –List the tracks in the database (track number and title) with the name of their secondary artist [9] (qry_day08_secondary_artists)

7 http://www.cse.msu.edu/~cse103 U:/msu/course/cse/103 Day 08, Slide 7 INNER JOINs and table aliases What SQL Keyword is used for aliases (remember calculated fields)? Same idea, only now rename tables to make JOINs easier –Also enables using same table twice, as in self-join Use table alias throughout ENTIRE query: SELECT MovieTitle, r.MovieID, Role FROM tbl_Roles AS r INNER JOIN tbl_Movies AS m ON r.MovieID = m.MovieID WHERE MovieTitle = "Blues Brothers, The"

8 http://www.cse.msu.edu/~cse103 U:/msu/course/cse/103 Day 08, Slide 8 Pencil and Paper Exercise on Relation Tables Does it matter which tables we join first? What does the shaded area in the upper left-hand corner of the worksheet represent? Which two tables should be joined next to complete the relational JOIN table? How many rows does the resulting JOIN have?

9 http://www.cse.msu.edu/~cse103 U:/msu/course/cse/103 Day 08, Slide 9 Relationships in Movies Work in your groups to identify relationships in the movies database –For each pair of tables, choose 1:1, 1:N, N:M, or not related –Look at primary keys; find where they're used as foreign keys –Use common sense… think "Does 1 X have many Y?" But always check that the keys support this idea!

10 http://www.cse.msu.edu/~cse103 U:/msu/course/cse/103 Day 08, Slide 10 Multiple-table Queries in Movies –Create a list of all the movies (titles and years) in which Robert Redford has appeared. [62] –Create a list of all the directors who have made “Frankenstein” movies, to include the title, year, and director’s name. [68] –Create a list of everyone who appeared in any movie made after 1990 who has since died [1961 with duplicates] How can we remove duplicates? [595]

11 http://www.cse.msu.edu/~cse103 U:/msu/course/cse/103 Day 08, Slide 11 Matching partial data: SQL OUTER JOIN…ON Outer Join –Useful with relationships where not all X have some property Y 1:1s always have this: not all tracks have a digital track; not all movies have a rating Some 1:Ns/M:Ns – not every person has an acting role; not every album has a label (because our data is incomplete) LEFT OUTER JOIN – combines records from two tables, but shows all records from the left table even if there are no matches with the right table e.g., All tracks, showing secondary artists where present: …tbl_Tracks LEFT JOIN tbl_Artists ON [Secondary Artist] = ArtistID Right Outer Join – (likewise … right table ….) Fields in the unmatched table(s) are set to NULL

12 http://www.cse.msu.edu/~cse103 U:/msu/course/cse/103 Day 08, Slide 12 OUTER JOIN in Design View Setup the query as if you were using an INNER JOIN Double-click the relationship to change –Look at options: first always corresponds to INNER JOIN (matched records only) –Second and third correspond to LEFT and RIGHT, but read carefully to see which table you want to include ALL from Access will use LEFT or RIGHT correctly; you don't have to know which is which

13 http://www.cse.msu.edu/~cse103 U:/msu/course/cse/103 Day 08, Slide 13 Music-day08 Multi-table queries Construct a list of Tracks with Track and Album Titles, arranged by album [376] qry_day08_titles Use that as a starting point for the next few queries (start over each time): –Add secondary artist names, still listing all tracks (qry_day08_titles_with_2 nd _artist) –Add labels for all tracks, even if unknown (qry_day08_titles_with_labels) –Find tracks that have no digital tracks [14] (qry_day08_titles_missing_digitracks)

14 http://www.cse.msu.edu/~cse103 U:/msu/course/cse/103 Day 08, Slide 14 Bridge Task 1.5 Everyone should come –Get next BT not passed, either 1.0 or 1.5 Primarily on material from Days 6-8 –Action queries and importing, append –INNER and OUTER JOIN in SQL and Access –Be sure homework is all done Have copies of databases we used in class and homework: –music_demo, music-day08, colleges&universities Bring photo ID!

15 http://www.cse.msu.edu/~cse103 U:/msu/course/cse/103 Day 08, Slide 15 Homework Prepare for BT 1.5 –Make sure that you have completed ALL imports into the various databases! Practice queries… ask questions like "Who starred in Action movies in 2003?" and write queries to solve them Reread textbook chapters 6-8 and 11 Understand relationships in our databases so you can join tables correctly


Download ppt "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."

Similar presentations


Ads by Google