CSE 154 LECTURE 14: MULTI-TABLE SQL QUERIES (JOINS)

Slides:



Advertisements
Similar presentations
What is a Database By: Cristian Dubon.
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 1 Committed to Shaping the Next Generation of IT Experts. Chapter 2: Relational Databases.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
3-1 Chapter 3 Data and Knowledge Management
Introduction to Databases CIS 5.2. Where would you find info about yourself stored in a computer? College Physician’s office Library Grocery Store Dentist’s.
Attribute databases. GIS Definition Diagram Output Query Results.
1 Chapter 2 Reviewing Tables and Queries. 2 Chapter Objectives Identify the steps required to develop an Access application Specify the characteristics.
July 14, 2015ICS 424: recap1 Relational Database Design: Recap of ICS 324.
Lecture 16: SQL and HTML tables
DATA, DATABASES, AND QUERIES Managing Data in Relational Databases CS1100Microsoft Access - Introduction1.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
DATA, DATABASES, AND QUERIES Managing Data in Relational Databases CS1100Microsoft Access - Introduction1 Created By Martin Schedlbauer
Appendix A Database Design CS Database design principles  database design: the act of deciding the schema for a database  database schema: a description.
Relational databases.  Retrieving data from a database requires pulling data from multiple tables  Tables relate to each other in distinct ways, modelled.
Database Design Concepts
Databases From A to Boyce Codd. What is a database? It depends on your point of view. For Manovich, a database is a means of structuring information in.
CS 405G: Introduction to Database Systems 16. Functional Dependency.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
1 Overview of Databases. 2 Content Databases Example: Access Structure Query language (SQL)
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
INFO 344 Web Tools And Development CK Wang University of Washington Spring 2014.
Example simpsons database
Database Management COP4540, SCS, FIU SQL (Continued) Querying Multiple Tables.
Programming using C# Joins SQL Injection Stored Procedures
CSC 405: Web Application And Engineering II7.1 Database Programming with SQL Aggregation and grouping with GROUP BY Aggregation and grouping with GROUP.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
Databases From A to Boyce Codd. What is a database? It depends on your point of view. For Manovich, a database is a means of structuring information in.
Normalization (Codd, 1972) Practical Information For Real World Database Design.
IST 210: ORGANIZATION OF DATA Chapter 1. Getting Started IST210 1.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
 Agenda 2/20/13 o Review quiz, answer questions o Review database design exercises from 2/13 o Create relationships through “Lookup tables” o Discuss.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
IST 220 Introduction to Databases Course Wrap-up.
Relational Databases.  In week 1 we looked at the concept of a key, the primary key is a column/attribute that uniquely identifies the rest of the data.
Pasewark & Pasewark 1 Access Lesson 3 Creating Queries Microsoft Office 2007: Introductory.
McGraw-Hill/Irwin The O’Leary Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Access 2002 Lab 3 Analyzing Tables and Creating.
Access Review. Access Access is a database application A database is a collection of records and files organized for a particular purpose Access supports.
Indexes and Views Unit 7.
Database Management Supplement 1. 2 I. The Hierarchy of Data Database File (Entity, Table) Record (info for a specific entity, Row) Field (Attribute,
DataSet Your Database student test score Database Connection Your program needs to establish a connection to the database. Click on “Add New Data Source.”
Lesson 2: Designing a Database and Creating Tables.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CTFS Workshop Shameema Esufali Asian data coordinator and technical resource for the network
Assignment 1 Uploaded to course website Due next Tuesday, Sep 1, at 11:59pm.
Relational Database in Access Student System As always please use speaker notes!
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 1: Introduction to IS2803 Rob Gleasure
Connecting (relating) Data Tables to get Custom Records (Queries) Database Basics.
QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
CSE 154 LECTURE 22:RELATIONAL DATABASES AND SQL. Relational databases relational database: A method of structuring data as tables associated to each other.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Chapter 1. Getting Started IST 210: Organization of Data IST2101.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
PHP and SQL Server: Connection IST 210: Organization of Data IST2101.
Lecture#7: Fun with SQL (Part 2)
Lecture 25: SQL and HTML tables
Databases and Information Management
Lecture 23: Multi-table SQL Queries (Joins)
Spreadsheets, Modelling & Databases
Lecture 24: Creating a Database and More joins
Database Assignment Write down your answers in word document with file name highlighting your name, student number, and class. E.g “95002”+”_”+ “03 class”+”_”+”name”,
Lecture 21: Multi-table SQL Queries (Joins)
Lecture 22: Creating a Database and More joins
Presentation transcript:

CSE 154 LECTURE 14: MULTI-TABLE SQL QUERIES (JOINS)

Exceptions for errors $db = new PDO("mysql:dbname=imdb_small", "jessica", "guinness"); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $rows = $db->query("SEEELECT * FROM movies WHERE year = 2000"); # kaboom! PHP using setAttribute, you can tell PDO to throw (generate) a PDOException when an error occurs the exceptions will appear as error messages on the page output you can catch the exception to gracefully handle the error

Related tables and keys idname idname 1234Krabappel 5678Hoover 9012Obourn idnameteacher_id 10001Computer Science Computer Science Computer Science Informatics student_idcourse_idgrade B C B A A D+ students teachers courses grades primary key: a column guaranteed to be unique for each record (e.g. Lisa Simpson's ID 888) foreign key: a column in table A storing a primary key value from table B (e.g. records in grades with student_id of 888 are Lisa's grades) normalizing: splitting tables to improve structure / redundancy (linked by unique IDs)normalizing

Giving names to tables SELECT s.name, g.* FROM students s JOIN grades g ON s.id = g.student_id WHERE g.grade <= 'C'; SQL namestudent_idcourse_idgrade Bart B- Bart C Milhouse B+ Lisa A+ Lisa A+ can give names to tables, like a variable name in Java to specify all columns from a table, write table.* ( grade column sorts alphabetically, so grades C or better are ones <= it)

Querying multi-table databases When we have larger datasets spread across multiple tables, we need queries that can answer high-level questions such as: What courses has Bart taken and gotten a B- or better? What courses have been taken by both Bart and Lisa? Who are all the teachers Bart has had? How many total students has Ms. Krabappel taught, and what are their names? To do this, we'll have to join data from several tables in our SQL queries.

Joining with ON clauses SELECT column(s) FROM table1 JOIN table2 ON condition(s)... JOIN tableN ON condition(s); SQL SELECT * FROM students JOIN grades ON id = student_id; SQL join: combines records from two or more tables if they satisfy certain conditions the ON clause specifies which records from each table are matched the rows are often linked by their key columns (id)

Join example SELECT * FROM students JOIN grades ON id = student_id; SQL idname student_idcourse_idgrade table. column can be used to disambiguate column names: SELECT * FROM students JOIN grades ON students.id = grades.student_id; SQL

Filtering columns in a join SELECT name, course_id, grade FROM students JOIN grades ON id = student_id; SQL namecourse_idgrade Bart10001B- Bart10002C Ralph10004D+ Milhouse10001B+ Lisa10002A+ Lisa10003A+

Filtered join (JOIN with WHERE) SELECT name, course_id, grade FROM students JOIN grades ON id = student_id WHERE name = 'Bart'; SQL namecourse_idgrade Bart10001B- Bart10002C FROM / JOIN glue the proper tables together, and WHERE filters the results what goes in the ON clause, and what goes in WHERE ? ON directly links columns of the joined tables WHERE sets additional constraints such as particular values ( 123, 'Bart' )

What's wrong with this? SELECT name, id, course_id, grade FROM students JOIN grades ON id = 123 WHERE id = student_id; SQL nameidcourse_idgrade Bart B- Bart C The above query produces the same rows as the previous one, but it is poor style. Why? The JOIN ON clause is poorly chosen. It doesn't really say what connects a grades record to a students record. They are related when they are for a student with the same id. Filtering out by a specific ID or name should be done in the WHERE clause, not JOIN ON.

Multi-way join SELECT c.name FROM courses c JOIN grades g ON g.course_id = c.id JOIN students bart ON g.student_id = bart.id WHERE bart.name = 'Bart' AND g.grade <= 'B-'; SQL name Computer Science 142 More than 2 tables can be joined, as shown above What does the above query represent? The names of all courses in which Bart has gotten a B- or better.

A suboptimal query Exercise: What courses have been taken by both Bart and Lisa? SELECT bart.course_id FROM grades bart JOIN grades lisa ON lisa.course_id = bart.course_id WHERE bart.student_id = 123 AND lisa.student_id = 888; SQL problem: requires us to know Bart/Lisa's Student IDs, and only spits back course IDs, not names. Write a version of this query that gets us the course names, and only requires us to know Bart/Lisa's names, not their IDs.

Improved query What courses have been taken by both Bart and Lisa? SELECT DISTINCT c.name FROM courses c JOIN grades g1 ON g1.course_id = c.id JOIN students bart ON g1.student_id = bart.id JOIN grades g2 ON g2.course_id = c.id JOIN students lisa ON g2.student_id = lisa.id WHERE bart.name = 'Bart' AND lisa.name = 'Lisa'; SQL

Practice queries What are the names of all teachers Bart has had? How many total students has Ms. Krabappel taught, and what are their names? SELECT DISTINCT t.name FROM teachers t JOIN courses c ON c.teacher_id = t.id JOIN grades g ON g.course_id = c.id JOIN students s ON s.id = g.student_id WHERE s.name = 'Bart'; SQL SELECT DISTINCT s.name FROM students s JOIN grades g ON s.id = g.student_id JOIN courses c ON g.course_id = c.id JOIN teachers t ON t.id = c.teacher_id WHERE t.name = 'Krabappel'; SQL

Designing a query Figure out the proper SQL queries in the following way: Which table(s) contain the critical data? ( FROM ) Which columns do I need in the result set? ( SELECT ) How are tables connected ( JOIN ) and values filtered ( WHERE )? Test on a small data set ( imdb_small ). Confirm on the real data set ( imdb ). Try out the queries first in the query tool. Write the PHP code to run those same queries. Make sure to check for SQL errors at every step!!

Example imdb database idfirst_namelast_namegender WilliamShatnerM BritneySpearsF SigourneyWeaverF... idnameyearrank Fight Club Meet the Parents Memento actor_idmovie_idrole Capt. James T. Kirk Sgt. T.J. Hooker Herself... actors moviesroles movie_idgenre Comedy Action Sci-Fi... idfirst_namelast_name 24758DavidFincher 66965JayRoach 72723WilliamShatner... director_idmovie_id movies_genres directors movies_directors also available, imdb_small with fewer records (for testing queries)

IMDb table relationships / ids

IMDb practice queries What are the names of all movies released in 1995? How many people played a part in the movie "Lost in Translation"? What are the names of all the people who played a part in the movie "Lost in Translation"? Who directed the movie "Fight Club"? How many movies has Clint Eastwood directed? What are the names of all movies Clint Eastwood has directed? What are the names of all directors who have directed at least one horror film? What are the names of every actor who has appeared in a movie directed by Christopher Nolan?