COMP 430 Intro. to Database Systems

Slides:



Advertisements
Similar presentations
Database Relationships in Access As you recall, the data in a database is stored in tables. In a relational database like Access, you can have multiple.
Advertisements

Chapter 4 Joining Multiple Tables
Ashley Ohmann June 20, * What is Custom SQL? * What can I do with it? * Join conditions * Unions and Self Joins * Ranks * Derived Tables.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
Murali Mani The Relational Model. Murali Mani Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #15 M.P. Johnson Stern School of Business, NYU Spring, 2005.
CS 255: Database System Principles slides: Variable length data and record By:- Arunesh Joshi( 107) Id: Cs257_107_ch13_13.7.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Relational databases.  Retrieving data from a database requires pulling data from multiple tables  Tables relate to each other in distinct ways, modelled.
1 Relational model concepts Key constraints Referential integrity constraint Steen Jensen, autumn 2013.
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
Relational Database Concepts. Let’s start with a simple example of a database application Assume that you want to keep track of your clients’ names, addresses,
Intro. to SQL DSC340 Mike Pangburn. Learning Objectives Understand the data-representation terminology underlying relational databases Understand core.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Inner Join vs. Outer Join
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
1 Intro to JOINs SQL INNER JOIN SQL OUTER JOIN SQL FULL JOIN SQL CROSS JOIN Intro to VIEWs Simple VIEWs Considerations about VIEWs VIEWs as filters ALTER.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
Relational Algebra – Part 2
WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.
While you are waiting for class to start... (1) Login to SQL Server 2012 Management Studio (2) Execute the file called “SQLLab3.sql”. It is located on.
Hassan Tariq MULTIPLE TABLES: SQL provides a convenient operation to retrieve information from multiple tables.SQL provides a convenient operation to.
COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!
Lectures 2&3: Introduction to SQL. Lecture 2: SQL Part I Lecture 2.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Query Processing – Implementing Set Operations and Joins Chap. 19.
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
COMP 430 Intro. to Database Systems Entity-Relationship Diagram Basics Slides use ideas from Chris Ré.
COMP 430 Intro. to Database Systems Grouping & Aggregation Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
SQL – join.
More SQL: Complex Queries,
Modeling Constraints Extracting constraints is what modeling is all about. But how do we express them? Examples: Keys: social security number uniquely.
Module 11: File Structure
COP5725 Database Management ER DIAGRAM AND RELATIONAL DATA MODEL
COMP 430 Intro. to Database Systems
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
Database Design Determinacy.
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
Lecture 2 (cont’d) & Lecture 3: Advanced SQL – Part I
David M. Kroenke and David J
Chapter 3 The Relational Database Model
Cse 344 January 12th –joins.
March 30th – intro to joins
Cse 344 January 10th –joins.
January 19th – Subqueries 2 and relational algebra
JOINS (Joinining multiple tables)
JOINs JOINs can be used to combine tables A CROSS JOIN B
Types of Joins Farrokh Alemi, Ph.D.
Relational Queries (query 12) Display vendor contact info (contact person and phone number) for inventory products (relationship query) Query: Inventory.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CMSC-461 Database Management Systems
Lecture 8: The E/R Model I
Lectures 7: Introduction to SQL 6
Lectures 3: Introduction to SQL 2
Introduction to Database Systems CSE 444 Lecture 02: SQL
Lectures 2: Introduction to SQL 1
Syllabus Introduction Website Management Systems
Lecture 08: E/R Diagrams and Functional Dependencies
JOINS (Joinining multiple tables)
Unit Relational Algebra 1
SELECT from Multiple Tables
Join Implementation How is it done? Copyright © Curt Hill.
Presentation transcript:

COMP 430 Intro. to Database Systems Joins

A bit of review Slides copied from earlier in course

Joining tables SELECT * FROM Product, Company p_name price manufacturer Gizmo 19.99 GizmoWorks Powergizmo 39.99 Widget WidgetsRUs HyperWidget 203.99 Hyper c_name address city state GizmoWorks 123 Gizmo St. Houston TX WidgetsRUs 20 Main St. New York NY Hyper 1 Mission Dr. San Francisco CA SELECT * FROM Product, Company WHERE manufacturer = c_name; Join condition p_name price manufacturer address city state Gizmo 19.99 GizmoWorks 123 Gizmo St. Houston TX Powergizmo 39.99 Widget WidgetsRUs 20 Main St. New York NY HyperWidget 203.99 Hyper 1 Mission Dr. San Francisco CA

Joining tables Product Company p_name price manufacturer Gizmo 19.99 GizmoWorks Powergizmo 39.99 Widget WidgetsRUs HyperWidget 203.99 Hyper c_name address city state GizmoWorks 123 Gizmo St. Houston TX WidgetsRUs 20 Main St. New York NY Hyper 1 Mission Dr. San Francisco CA This is simplest & most common kind of join – an inner join. See other kinds later. p_name price manufacturer address city state Gizmo 19.99 GizmoWorks 123 Gizmo St. Houston TX Powergizmo 39.99 Widget WidgetsRUs 20 Main St. New York NY HyperWidget 203.99 Hyper 1 Mission Dr. San Francisco CA

Joins – forgetting the join condition SELECT * FROM Product, Company; p_name price manufacturer c_name address city state Gizmo 19.99 GizmoWorks 123 Gizmo St. Houston TX WidgetsRUs 20 Main St. New York NY Hyper 1 Mission Dr. San Francisco CA Powergizmo 39.99 Widget … All combinations of records! – Cross-product of tables.

Selection & Projection SELECT p_name FROM Product, Company WHERE manufacturer = c_name AND state = ‘TX’; Join condition Filter condition p_name price manufacturer address city state Gizmo 19.99 GizmoWorks 123 Gizmo St. Houston TX Powergizmo 39.99 Selection p_name Gizmo Powergizmo Projection

So what else is there?

Inner join syntax Old style: Current, preferred style: SELECT * FROM Product, Company WHERE manufacturer = c_name; SELECT * FROM Product INNER JOIN Company ON manufacturer = c_name; SELECT * FROM Company INNER JOIN Product ON manufacturer = c_name; Shorter Easier to find tables Equal status for tables – symmetry Can’t forget join condition Join condition separated from filter conditions Consistent with other joins

Two versions equivalent SELECT * FROM Product, Company WHERE manufacturer = c_name; SELECT * FROM Product INNER JOIN Company ON manufacturer = c_name; Product (p_name, price, manufacturer) Company (c_name, address, city, state) Result (p_name, price, manufacturer, c_name, address, city, state) Previously lied a little. Both fields present.

Inner join – special case Both tables use the same attribute name. Product (p_name, price, c_name) Company (c_name, address, city, state) SELECT * FROM Product INNER JOIN Company ON Product.c_name = Company.c_name; Join condition is simple equality. Equi-join Result (p_name, price, Product.c_name, Company.c_name, address, city, state) SELECT * FROM Product INNER JOIN Company USING (c_name); Can list multiple attributes. Result (p_name, price, c_name, address, city, state)

Inner join – another special case Product (p_name, price, c_name) Company (c_name, address, city, state) Considered dangerous, since it constraint is implicit, and attribute name sets can change. Don’t use. SELECT * FROM Product INNER JOIN Company USING (c_name); Result (p_name, price, c_name, address, city, state) SELECT * FROM Product NATURAL INNER JOIN Company; Assumes equality of same-named attributes. Equi-join Result (p_name, price, c_name, address, city, state)

What about dangling tuples? Company Product c_name state GizmoWorks TX WidgetsRUs NY Hyper CA NewCo p_name manufacturer Gizmo GizmoWorks Powergizmo Widget WidgetsRUs HyperWidget Hyper c_name state p_name manufacturer GizmoWorks TX Gizmo Powergizmo WidgetsRUs NY Widget Hyper CA HyperWidget NewCo is forgotten in join, since it has no products. SELECT * FROM Company INNER JOIN Product ON manufacturer = c_name;

Left outer join Company Product c_name state GizmoWorks TX WidgetsRUs NY Hyper CA NewCo p_name manufacturer Gizmo GizmoWorks Powergizmo Widget WidgetsRUs HyperWidget Hyper c_name state p_name manufacturer GizmoWorks TX Gizmo Powergizmo WidgetsRUs NY Widget Hyper CA HyperWidget NewCo NULL “LEFT” because result includes dangling tuples from the left table.. Original tables have no data. SELECT * FROM Company LEFT OUTER JOIN Product ON manufacturer = c_name;

Right outer join Company Product c_name state GizmoWorks TX WidgetsRUs NY Hyper CA p_name manufacturer Gizmo GizmoWorks Powergizmo Widget WidgetsRUs HyperWidget Hyper c_name state p_name manufacturer GizmoWorks TX Gizmo Powergizmo WidgetsRUs NY Widget Hyper CA HyperWidget Useless in this example. Referential integrity guarantees Product has no dangling tuples. SELECT * FROM Company RIGHT OUTER JOIN Product ON manufacturer = c_name;

Assume no referential integrity now. Right outer join Company Product c_name state GizmoWorks TX WidgetsRUs NY Hyper CA p_name manufacturer Gizmo GizmoWorks Powergizmo Widget WidgetsRUs HyperWidget Hyper NewThing NULL c_name state p_name manufacturer GizmoWorks TX Gizmo Powergizmo WidgetsRUs NY Widget Hyper CA HyperWidget NULL NewThing Assume no referential integrity now. SELECT * FROM Company RIGHT OUTER JOIN Product ON manufacturer = c_name;

Includes dangling tuples from both sides. Full outer join Company Product c_name state GizmoWorks TX WidgetsRUs NY Hyper CA NewCo p_name manufacturer Gizmo GizmoWorks Powergizmo Widget WidgetsRUs HyperWidget Hyper NewThing NULL c_name state p_name manufacturer GizmoWorks TX Gizmo Powergizmo WidgetsRUs NY Widget Hyper CA HyperWidget NewCo NULL NewThing Includes dangling tuples from both sides. SELECT * FROM Company FULL OUTER JOIN Product ON manufacturer = c_name;

Cross join SELECT * FROM Product CROSS JOIN Company; CROSS JOIN doesn’t have join condition. p_name price manufacturer c_name address city state Gizmo 19.99 GizmoWorks 123 Gizmo St. Houston TX WidgetsRUs 20 Main St. New York NY Hyper 1 Mission Dr. San Francisco CA Powergizmo 39.99 Widget … All combinations of records! – Cross-product of tables.

A few notes There are various short-hands, e.g., JOIN = INNER JOIN, LEFT JOIN = LEFT OUTER JOIN. Outer joins also allow NATURAL, USING. Left outer join of tables A,B = Right outer join of tables B,A. SQLite doesn’t implement Right & Full outer joins. These “joins” describe semantics. Other “joins” describe implementation.

Visual summary Of inner & outer joins Examples adapted from Coding Horror.

Records matching both A and B. id data 1 A 2 B 3 C 4 D id A.data B.data 2 B W 4 D X B id data 2 W 4 X 6 Y 8 Z SELECT * FROM A INNER JOIN B ON A.id = B.id; Records matching both A and B.

FULL OUTER JOIN B ON A.id = B.id; Records matching A or B. data 1 A 2 B 3 C 4 D id A.data B.data 1 A 2 B W 3 C 4 D X 6 Y 8 Z B id data 2 W 4 X 6 Y 8 Z SELECT * FROM A FULL OUTER JOIN B ON A.id = B.id; Records matching A or B.

Records matching either A or B, but not both. id data 1 A 2 B 3 C 4 D id A.data B.data 1 A 3 C 6 Y 8 Z B id data 2 W 4 X 6 Y 8 Z SELECT * FROM A FULL OUTER JOIN B ON A.id = B.id WHERE A.data IS NULL OR B.data IS NULL; Records matching either A or B, but not both.

LEFT OUTER JOIN B ON A.id = B.id; Records matching A. data 1 A 2 B 3 C 4 D id A.data B.data 1 A 2 B W 3 C 4 D X B id data 2 W 4 X 6 Y 8 Z SELECT * FROM A LEFT OUTER JOIN B ON A.id = B.id; Records matching A.

Records matching only A. id data 1 A 2 B 3 C 4 D id A.data B.data 1 A 3 C B id data 2 W 4 X 6 Y 8 Z SELECT * FROM A LEFT OUTER JOIN B ON A.id = B.id WHERE B.data IS NULL; Records matching only A.