DS Introduction to SQL Part 2 Multi-table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Similar documents
Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Lecture 2: Introduction to SQL

Lecture 3: SQL Part II

Big Data Processing Technologies. Chentao Wu Associate Professor Dept. of Computer Science and Engineering

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Polls on Piazza. Open for 2 days Outline today: Next time: "witnesses" (traditionally students find this topic the most difficult)

Lectures 2&3: Introduction to SQL

Introduction to Database Systems CSE 414. Lecture 3: SQL Basics

INF 212 ANALYSIS OF PROG. LANGS SQL AND SPREADSHEETS. Instructors: Crista Lopes Copyright Instructors.

Agenda. Discussion. Database/Relation/Tuple. Schema. Instance. CSE 444: Database Internals. Review Relational Model

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2006 Lecture 3 - Relational Model

COMP 430 Intro. to Database Systems

Introduction to Database Systems CSE 414. Lecture 3: SQL Basics

Before we talk about Big Data. Lets talk about not-so-big data

Lecture 3 Additional Slides. CSE 344, Winter 2014 Sudeepa Roy

Introduction to Database Systems CSE 444

SQL. Structured Query Language

SQL. Assit.Prof Dr. Anantakul Intarapadung

Announcements. Agenda. Database/Relation/Tuple. Schema. Discussion. CSE 444: Database Internals

Announcements. Agenda. Database/Relation/Tuple. Discussion. Schema. CSE 444: Database Internals. Room change: Lab 1 part 1 is due on Monday

CSE 344 JANUARY 8 TH SQLITE AND JOINS

CSE 344 JANUARY 5 TH INTRO TO THE RELATIONAL DATABASE

Introduction to Relational Databases

Database Systems CSE 303. Lecture 02

Database Systems CSE 303. Lecture 02

CS 564 Midterm Review

Database Systems CSE 414

Principles of Database Systems CSE 544. Lecture #2 SQL The Complete Story

Introduction to Databases CSE 414. Lecture 2: Data Models

SQLite, Firefox, and our small IMDB movie database. CS3200 Database design (sp18 s2) Version 1/17/2018

Introduction to Data Management CSE 414

CMPT 354: Database System I. Lecture 3. SQL Basics

Introduction to SQL Part 2 by Michael Hahsler Based on slides for CS145 Introduction to Databases (Stanford)

Introduction to Data Management CSE 344. Lecture 2: Data Models

Data Informatics. Seon Ho Kim, Ph.D.

Introduction to Data Management CSE 344

MTAT Introduction to Databases

L07: SQL: Advanced & Practice. CS3200 Database design (sp18 s2) 1/11/2018

The Entity-Relationship Model (ER Model) - Part 2

CSE 544 Principles of Database Management Systems

CSC 261/461 Database Systems Lecture 5. Fall 2017

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Announcements. Multi-column Keys. Multi-column Keys (3) Multi-column Keys. Multi-column Keys (2) Introduction to Data Management CSE 414

CSE 544 Principles of Database Management Systems

Keys, SQL, and Views CMPSCI 645

ENTITY-RELATIONSHIP MODEL. CS 564- Spring 2018

The Relational Data Model

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Announcements. Multi-column Keys. Multi-column Keys. Multi-column Keys (3) Multi-column Keys (2) Introduction to Data Management CSE 414

1 (10) 2 (8) 3 (12) 4 (14) 5 (6) Total (50)

Introduction to Database Systems. The Relational Data Model

Introduction to Database Systems. The Relational Data Model. Werner Nutt

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

CPSC 504 Background (aka, all you need to know about databases for this course in two lectures) Rachel Pottinger January 8 and 12, 2015

Database Management Systems. Chapter 3 Part 1

Relational data model

The Relational Model. Chapter 3

The Relational Data Model. Data Model

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

Entity/Relationship Modelling

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

Principles of Database Systems CSE 544. Lecture #1 Introduction and SQL

The Relational Model of Data (ii)

Database design and implementation CMPSCI 645. Lecture 04: SQL and Datalog

The Relational Model 2. Week 3

Principles of Database Systems CSE 544. Lecture #1 Introduction and SQL

The Relational Model

Lecture 16. The Relational Model

The Entity-Relationship Model (ER Model) - Part 1

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

Administrative notes. Levels of Abstraction. Overview of the next two classes. Schema and Instances. Conceptual Database Design

CSC 261/461 Database Systems Lecture 13. Fall 2017

CSC 261/461 Database Systems Lecture 7

Lecture 4. Lecture 4: The E/R Model

CSC 261/461 Database Systems Lecture 6. Spring 2017 MW 3:25 pm 4:40 pm January 18 May 3 Dewey 1101

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

COMP 430 Intro. to Database Systems

L12: ER modeling 5. CS3200 Database design (sp18 s2) 2/22/2018

Announcements. Outline UNIQUE. (Inner) joins. (Inner) Joins. Database Systems CSE 414. WQ1 is posted to gradebook double check scores

CS425 Midterm Exam Summer C 2012

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

CS 2451 Database Systems: More SQL

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

The Relational Model. Suan Lee

Database Systems ( 資料庫系統 )

Principles of Database Systems CSE 544p. Lecture #1 September 28, 2011

Lecture 5. Lecture 5: The E/R Model

CS W Introduction to Databases Spring Computer Science Department Columbia University

The Relational Model

COMP 244 DATABASE CONCEPTS AND APPLICATIONS

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

Database Systems CSE 414

Announcements. PS 3 is out (see the usual place on the course web) Be sure to read my notes carefully Also read. Take a break around 10:15am

CSE 344 JULY 30 TH DB DESIGN (CH 4)

Administrivia. The Relational Model. Review. Review. Review. Some useful terms

Transcription:

DS 1300 - Introduction to SQL Part 2 Multi-table Queries By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

What you will learn about in this section 1. Foreign key constraints 2. Joins: basics 3. Joins: SQL semantics 4. Activities: Multi-table queries 2

Foreign Key Constraints Suppose we have the following schema: Students(sid: text, name: text, gpa: real) Enrolled(student_id: text, cid: text, grade: real) And we want to impose the following constraint: Only existing students may enroll in courses i.e. a student must appear in the Students table to enroll in a class Students sid name gpa 101 Bob 3.2 123 Mary 3.8 Enrolled student_id cid grade 123 564 A 123 537 A+ Note: student_id alone is not a key- what is? We say that student_id is a foreign key that refers to Students

Declaring Foreign Keys Students(sid: text, name: text, gpa: real) Enrolled(student_id: text, cid: text, grade: text) CREATE TABLE Enrolled( student_id CHAR(20), cid CHAR(20), grade CHAR(10), PRIMARY KEY (student_id, cid), FOREIGN KEY (student_id) REFERENCES Students ) Primary key Foreign key

Foreign Keys and Update Operations Students(sid: text, name: text, gpa: real) Enrolled(student_id: text, cid: text, grade: text) What if we insert a tuple into Enrolled, but no corresponding student? INSERT is rejected (foreign keys are constraints)! What if we delete a student? 1. Disallow the delete 2. Remove all of the courses for that student 3. SQL allows a third via NULL (not yet covered) SQLite: Enable foreign keys with PRAGMA foreign_keys = ON; DB Browser: check Foreign Keys in Edit Pragma

Keys and Foreign Keys Company CName StockPrice Country GizmoWorks 25 USA Canon 65 Japan Hitachi 15 Japan Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon What is a foreign key vs. a key here? MultiTouch $203.99 Household Hitachi 6

Keys and Foreign Keys Company(CName, StockPrice, Country) Product(PName, Price, Category, Manufacturer) This example uses natural keys. Often surrogate keys are used instead: Company(Cnumber, CName, StockPrice, Country) Product(Pnumber, Pname, Price, Category, ManufNumber) Why? Why do we use SMU IDs and Social Security Numbers? 7

Joins Product(PName, Price, Category, Manufacturer) Company(CName, StockPrice, Country) Ex: Find all products under $200 manufactured in Japan; return their names and prices. This will need information from both tables... 8

Joins Product(PName, Price, Category, Manufacturer) Company(CName, StockPrice, Country) Ex: Find all products under $200 manufactured in Japan; return their names and prices. SELECT PName, Price FROM Product, Company WHERE Manufacturer = CName AND Country= Japan AND Price <= 200 A join between tables returns all unique combinations of their tuples which meet some specified join condition 9

Joins Product(PName, Price, Category, Manufacturer) Company(CName, StockPrice, Country) Several equivalent ways to write a basic join in SQL: SELECT PName, Price FROM Product, Company WHERE Manufacturer = CName AND Country= Japan AND Price <= 200 SELECT PName, Price FROM Product JOIN Company ON Manufacturer = Cname WHERE Price <= 200 AND Country= Japan 10

Joins Product PName Price Category Manuf Gizmo $19 Gadgets GWorks Powergizmo $29 Gadgets GWorks SingleTouch $149 Photography Canon MultiTouch $203 Household Hitachi Company Cname Stock Country GWorks 25 USA Canon 65 Japan Hitachi 15 Japan SELECT PName, Price FROM Product JOIN Company ON Manufacturer = Cname WHERE Price <= 200 AND Country= Japan PName Price SingleTouch $149.99 11

An Example of SQL Semantics R A 1 3 SELECT R.A FROM R, S WHERE R.A = S.B A 3 3 S B C 2 3 3 4 3 5 12

An Example of SQL Semantics R S A 1 3 B C 2 3 3 4 3 5 Cross Product SELECT R.A FROM R, S WHERE R.A = S.B R x S A B C 1 2 3 1 3 4 1 3 5 3 2 3 3 3 4 3 3 5 Apply Selections / Conditions A 3 3 A B C 3 3 4 3 3 5 Apply Projection 13

Tuple Variable Ambiguity in Multi-Table Person(name, address, worksfor) Company(name, address) SELECT DISTINCT name, address FROM Person, Company WHERE worksfor = name Which address does this refer to? Which name s?? 15

Tuple Variable Ambiguity in Multi-Table Person(name, address, worksfor) Company(name, address) Both equivalent ways to resolve variable ambiguity SELECT DISTINCT Person.name, Person.address FROM Person, Company WHERE Person.worksfor = Company.name SELECT DISTINCT p.name, p.address FROM Person p, Company c WHERE p.worksfor = c.name 16

A Note on Semantics semantics is not equal to execution order The preceding slides show what a join means Not actually how the DBMS executes it under the covers

Activities 1. Create the product/company database from the slide set. Add the following relation Purchase(id, product, buyer). with the appropriate foreign key constraints and add some data. 2. Find all countries that manufacture some product in the Gadgets category (shows each country only once). 3. Find all products that are manufactured in the US sorted by price. 4. For a given buyer, in how many different countries are the products she purchases manufactured? 20